admin管理员组文章数量:1326278
I am using WebPack + React + Jest for my application and I have set resolve.alias = { app: "/path/to/app" }
in my configuration file.
In React, I can use this path to do require("app/ponent") and obtain the file at "/path/to/app/ponent.js" correctly.
When running JEST tests this alias is not recognized, neither in the tests, nor for the imported modules. All of this works when running the app but not the tests with jest-cli.
I am using WebPack + React + Jest for my application and I have set resolve.alias = { app: "/path/to/app" }
in my configuration file.
In React, I can use this path to do require("app/ponent") and obtain the file at "/path/to/app/ponent.js" correctly.
When running JEST tests this alias is not recognized, neither in the tests, nor for the imported modules. All of this works when running the app but not the tests with jest-cli.
Share Improve this question edited Dec 23, 2014 at 12:26 Brigand 86.3k20 gold badges167 silver badges173 bronze badges asked Dec 22, 2014 at 12:39 gbbrgbbr 3073 silver badges9 bronze badges3 Answers
Reset to default 4jest-cli
lives in a world separate from Webpack so it cannot work that way.
Use babel-plugin-module-resolver to handle aliasing.
Jestpack might help as it will allow you to run your tests after they've been built with Webpack.
I use mocha to test react, but I think it also work for jest.
You should use 2 package: mock-require and proxyquire.
Assuming you have a js file like this:
app.js
import action1 from 'actions/youractions';
export function foo() { console.log(action1()) };
And your test code should write like this:
app.test.js
import proxyquire from 'proxyquire';
import mockrequire from 'mock-require';
before(() => {
// mock the alias path, point to the actual path
mockrequire('actions/youractions', 'your/actual/action/path/from/your/test/file');
// or mock with a function
mockrequire('actions/youractions', {actionMethod: () => {...}));
let app;
beforeEach(() => {
app = proxyquire('./app', {});
});
//test code
describe('xxx', () => {
it('xxxx', () => {
...
});
});
files tree
app.js
|- test
|- app.test.js
First mock the alias path by mock-require in before function, and mock your test object by proxyquire in beforeEach function.
本文标签: javascriptCan not use resolvealias for JEST testingStack Overflow
版权声明:本文标题:javascript - Can not use resolve.alias for JEST testing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742213046a2434067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论