admin管理员组文章数量:1279016
I was wondering how I would incorporate the esm
package with jest on a node backend.
I tried setting up a setup file with require("esm")
and require("esm")(module)
at the very top of the file, but it's still giving me the SyntaxError: Unexpected token
error.
I would have previously used node -r esm
but jest doesn't support this.
I was wondering how I would incorporate the esm
package https://www.npmjs./package/esm with jest on a node backend.
I tried setting up a setup file with require("esm")
and require("esm")(module)
at the very top of the file, but it's still giving me the SyntaxError: Unexpected token
error.
I would have previously used node -r esm
but jest doesn't support this.
2 Answers
Reset to default 8 +50When you perform require("esm")(module)
, think of it as you are creating an esm-transformer function that is pending a file to be transformed into an ES module.
Here's my attempt with node v8+ with:
default
jest
configurationdefault
esm
configuration
utils-1.js:
export const add = (a, b) => a + b;
utils-2.js:
export const multiAdd = array => array.reduce((sum, next) => sum + next, 0)
_test_/utils-1.assert.js
import { add } from '../utils-1';
describe('add(a,b)', () => {
it('should return the addtion of its two inputs', () => {
expect(add(1,2)).toBe(3);
});
});
_test_/utils-2.assert.js
import { multiAdd } from '../utils-2';
describe('multiAdd(<Number[]>)', () => {
it('should return a summation of all array elements', () => {
expect(multiAdd([1,2,3,4])).toBe(10);
})
});
_test_/utils.test.js
const esmImport = require('esm')(module);
const utils_1 = esmImport('./utils-1.assert')
const utils_2 = esmImport('./utils-2.assert')
Hope this helps!
In case anyone is still looking, the accepted answer does require a bit of effort.
You can also use the esm
package with jest
by transforming esm files back to cjs using jest-esm-transformer-2
(or jest-esm-transformer
if you are using jest
< 28)
- Install the package
npm i jest-esm-transformer-2 --save-dev
- Create your
jest.config.js
in the root directory and configure code transform to usejest-esm-transformer-2
const config = {
transform: {
'.js': 'jest-esm-transformer-2'
}
}
module.exports = config
- Run
jest
with no flags etc.
本文标签: javascriptnodeusing jest with esm packageStack Overflow
版权声明:本文标题:javascript - node - using jest with esm package - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741267967a2368840.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论