diff --git a/packages/testlab/src/__tests__/integration/test-sandbox.integration.ts b/packages/testlab/src/__tests__/integration/test-sandbox.integration.ts index dd6bb6dbbe9e..586c1a4d6a52 100644 --- a/packages/testlab/src/__tests__/integration/test-sandbox.integration.ts +++ b/packages/testlab/src/__tests__/integration/test-sandbox.integration.ts @@ -3,9 +3,9 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {TestSandbox, expect} from '../..'; +import {pathExists, readFile, remove, writeJSON} from 'fs-extra'; import {resolve} from 'path'; -import {remove, pathExists, readFile, writeJSON} from 'fs-extra'; +import {expect, TestSandbox} from '../..'; const FIXTURES = resolve(__dirname, '../../../fixtures'); @@ -65,6 +65,14 @@ describe('TestSandbox integration tests', () => { expect(fileContents.pop()).to.equal(sourceMapString); }); + it('creates a JSON file in the sandbox', async () => { + await sandbox.writeJsonFile('data.json', {key: 'value'}); + const fullPath = resolve(path, 'data.json'); + expect(await pathExists(fullPath)).to.be.True(); + const content = await readFile(fullPath, 'utf-8'); + expect(content).to.equal('{\n "key": "value"\n}\n'); + }); + it('resets the sandbox', async () => { const file = 'test.js'; const resolvedFile = resolve(__dirname, '../fixtures/test.js'); diff --git a/packages/testlab/src/test-sandbox.ts b/packages/testlab/src/test-sandbox.ts index fdadf5dfe045..da1f22d0471b 100644 --- a/packages/testlab/src/test-sandbox.ts +++ b/packages/testlab/src/test-sandbox.ts @@ -3,16 +3,17 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {resolve, parse} from 'path'; import { + appendFile, copy, - ensureDirSync, emptyDir, - remove, ensureDir, + ensureDirSync, pathExists, - appendFile, + remove, + writeJson, } from 'fs-extra'; +import {parse, resolve} from 'path'; /** * TestSandbox class provides a convenient way to get a reference to a @@ -105,4 +106,17 @@ export class TestSandbox { await appendFile(dest, `\n//# sourceMappingURL=${srcMap}`); } } + + /** + * Creates a new file and writes the given data serialized as JSON. + * + * @param dest - Destination filename, optionally including a relative path. + * @param data - The data to write. + */ + async writeJsonFile(dest: string, data: unknown): Promise { + dest = resolve(this.path, dest); + const destDir = parse(dest).dir; + await ensureDir(destDir); + return writeJson(dest, data, {spaces: 2}); + } }