From 74c41c571faf15a4d3a2fbe1f368aca8b6fde7ba Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 22 Nov 2019 20:42:43 +0300 Subject: [PATCH] feat: improve tests --- templates/test/fixtures/foo.js | 2 +- templates/test/fixtures/simple.js | 2 ++ templates/test/helpers/execute.js | 22 ++++++++++++++++++++++ templates/test/helpers/getCompiler.js | 6 ++---- templates/test/loader.test.js | 14 ++++++++++---- 5 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 templates/test/helpers/execute.js diff --git a/templates/test/fixtures/foo.js b/templates/test/fixtures/foo.js index 81afa31..d02ba54 100644 --- a/templates/test/fixtures/foo.js +++ b/templates/test/fixtures/foo.js @@ -1 +1 @@ -console.log('foo'); +export default 'foo'; diff --git a/templates/test/fixtures/simple.js b/templates/test/fixtures/simple.js index 4ad6554..0b5c35c 100644 --- a/templates/test/fixtures/simple.js +++ b/templates/test/fixtures/simple.js @@ -1,3 +1,5 @@ import foo from './foo'; +__export__ = foo; + export default foo; diff --git a/templates/test/helpers/execute.js b/templates/test/helpers/execute.js new file mode 100644 index 0000000..866001a --- /dev/null +++ b/templates/test/helpers/execute.js @@ -0,0 +1,22 @@ +import Module from 'module'; +import path from 'path'; + +const parentModule = module; + +export default (code) => { + const resource = 'test.js'; + const module = new Module(resource, parentModule); + // eslint-disable-next-line no-underscore-dangle + module.paths = Module._nodeModulePaths( + path.resolve(__dirname, '../fixtures') + ); + module.filename = resource; + + // eslint-disable-next-line no-underscore-dangle + module._compile( + `let __export__;${code};module.exports = __export__;`, + resource + ); + + return module.exports; +}; diff --git a/templates/test/helpers/getCompiler.js b/templates/test/helpers/getCompiler.js index 2575d09..76a233a 100644 --- a/templates/test/helpers/getCompiler.js +++ b/templates/test/helpers/getCompiler.js @@ -1,7 +1,7 @@ import path from 'path'; import webpack from 'webpack'; -import memfs from 'memfs'; +import { createFsFromVolume, Volume } from 'memfs'; export default (fixture, loaderOptions = {}, config = {}) => { const fullConfig = { @@ -34,9 +34,7 @@ export default (fixture, loaderOptions = {}, config = {}) => { const compiler = webpack(fullConfig); if (!config.outputFileSystem) { - const outputFileSystem = memfs; - - outputFileSystem.vol.reset(); + const outputFileSystem = createFsFromVolume(new Volume()); // Todo remove when we drop webpack@4 support outputFileSystem.join = path.join.bind(path); diff --git a/templates/test/loader.test.js b/templates/test/loader.test.js index b21141c..2ea5ef6 100644 --- a/templates/test/loader.test.js +++ b/templates/test/loader.test.js @@ -1,13 +1,19 @@ -import { getCompiler, compile, normalizeErrors, readAsset } from './helpers'; +import { + compile, + execute, + getCompiler, + normalizeErrors, + readAsset, +} from './helpers'; describe('loader', () => { it('should work', async () => { const compiler = getCompiler('simple.js'); const stats = await compile(compiler); - expect(readAsset('main.bundle.js', compiler, stats)).toMatchSnapshot( - 'result' - ); + expect( + execute(readAsset('main.bundle.js', compiler, stats)) + ).toMatchSnapshot('result'); expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot( 'warnings' );