diff --git a/packages/testlab/src/test-sandbox.ts b/packages/testlab/src/test-sandbox.ts index c8e7723daa80..2138614c17a6 100644 --- a/packages/testlab/src/test-sandbox.ts +++ b/packages/testlab/src/test-sandbox.ts @@ -58,6 +58,16 @@ export class TestSandbox { */ async reset(): Promise { this.validateInst(); + + // Decache files from require's cache so future tests aren't affected incase + // a file is recreated in sandbox with the same file name but different + // contents after resetting the sandbox. + for (const key in require.cache) { + if (key.startsWith(this.path)) { + delete require.cache[key]; + } + } + await emptyDir(this.path); } diff --git a/packages/testlab/test/integration/test-sandbox.integration.ts b/packages/testlab/test/integration/test-sandbox.integration.ts index e08ee57b9ef8..fc419deee096 100644 --- a/packages/testlab/test/integration/test-sandbox.integration.ts +++ b/packages/testlab/test/integration/test-sandbox.integration.ts @@ -5,7 +5,7 @@ import {TestSandbox, expect} from '../..'; import {resolve} from 'path'; -import {remove, pathExists, readFile} from 'fs-extra'; +import {remove, pathExists, readFile, writeJSON} from 'fs-extra'; describe('TestSandbox integration tests', () => { let sandbox: TestSandbox; @@ -63,6 +63,25 @@ describe('TestSandbox integration tests', () => { expect(fileContents.pop()).to.equal(sourceMapString); }); + it('resets the sandbox', async () => { + const file = 'test.js'; + const resolvedFile = resolve(__dirname, '../fixtures/test.js'); + await sandbox.copyFile(resolvedFile); + await sandbox.reset(); + expect(await pathExists(resolve(path, file))).to.be.False(); + }); + + it('decaches files from npm require when sandbox is reset', async () => { + const file = 'test.json'; + await writeJSON(resolve(path, file), {x: 1}); + const data = require(resolve(path, file)); + expect(data).to.be.eql({x: 1}); + await sandbox.reset(); + await writeJSON(resolve(path, file), {x: 2}); + const data2 = require(resolve(path, file)); + expect(data2).to.be.eql({x: 2}); + }); + it('deletes the test sandbox', async () => { await sandbox.delete(); expect(await pathExists(path)).to.be.False();