Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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');
Expand Down
22 changes: 18 additions & 4 deletions packages/testlab/src/test-sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<void> {
dest = resolve(this.path, dest);
const destDir = parse(dest).dir;
await ensureDir(destDir);
return writeJson(dest, data, {spaces: 2});
}
}