As a JS library author I pile and publish my source code in the following formats:
- monJS (
/lib/index.js
) - ES (
/es/index.js
) - UMD (
/dist/index.js
)
My unit tests cover my source code and I trust my bundling/piling tools to generate working bundles.
Then I read this enlightening post by React team where they explain that they run part of their unit tests against the bundled version of the library.
They introduced a test-build-prod
which runs Jest with a special configuration file which replace the original import declarations in the test to point the bundled files using Jest's moduleNameMapper
option.
It's cool but a bit overwhelming to replicate in my tiny open source side projects.
Before I give this a try, is there any other tool or more portable solution I should consider to run the same test I run on my source code against the piled bundles?
As a JS library author I pile and publish my source code in the following formats:
- monJS (
/lib/index.js
) - ES (
/es/index.js
) - UMD (
/dist/index.js
)
My unit tests cover my source code and I trust my bundling/piling tools to generate working bundles.
Then I read this enlightening post by React team where they explain that they run part of their unit tests against the bundled version of the library.
They introduced a test-build-prod
which runs Jest with a special configuration file which replace the original import declarations in the test to point the bundled files using Jest's moduleNameMapper
option.
It's cool but a bit overwhelming to replicate in my tiny open source side projects.
Before I give this a try, is there any other tool or more portable solution I should consider to run the same test I run on my source code against the piled bundles?
Share Improve this question edited Aug 11, 2020 at 17:19 Andrea Carraro asked Aug 11, 2018 at 11:42 Andrea CarraroAndrea Carraro 10.5k5 gold badges39 silver badges59 bronze badges1 Answer
Reset to default 10I'll share the solution I finally went with, which is the same adopted by React team but on small scale.
I added a special npm script to run my unit tests against each piled bundle with a different Jest configuration for each bundle:
{
"test:bundles": "jest --config ./jest/es.config.js && jest --config ./jest/lib.config.js && jest --config ./jest/dist.config.js"
}
Each Jest configuration file extends default Jest configuration and declares a moduleNameMapper
object plus a rootDir
property like:
// jest/lib.config.js
const pkg = require('../package.json');
module.exports = Object.assign({}, pkg.jest, {
rootDir: '../',
moduleNameMapper: {
'/src/index$': '<rootDir>/lib/index', // path of "CommonJS" bundle
},
});
moduleNameMapper
will make sure that the same tests used for source code will run against an exported bundle since Jest will transform the import statements on runtime like:
import myLibrary from './src/index';
// transformed into:
import myLibrary from '../lib/index';
The only point to pay attention to is making tests import statements needing remapping easily recognizable by moduleNameMapper
regex.
If import from './index'
is not unique enough in test the files, it might be rewritten as import from '../src/index'
.