admin管理员组文章数量:1289834
I have a React ponent. For good or bad, I use the Routes
global object exposed by js-routes
- a gem for Rails. I have a snapshot test using Jest which tests my simple ponent that happens to use the Routes
global.
In my test I want to mock Routes
. So when my ponent calls Routes.some_path()
, I can just return some arbitrary string.
The error that I mainly get is Cannot find name 'Routes'.
.
Here is my setup:
package.json
"jest": {
"globals": {
"ts-jest": {
"enableTsDiagnostics": true
}
},
"testEnvironment": "jsdom",
"setupFiles": [
"<rootDir>/app/javascript/__tests__/setupRoutes.ts"
]
...
setupRoutes.ts (This seemed like the most popular solution)
const globalAny:any = global;
globalAny.Routes = {
some_path: () => {},
};
myTest.snapshot.tsx
import * as React from 'react';
import {shallow} from 'enzyme';
import toJson from 'enzyme-to-json';
import MyComponent from 'MyComponent';
describe('My Component', () => {
let ponent;
beforeEach(() => {
ponent = shallow(<MyComponent />);
});
it('should render correctly', () => {
expect(toJson(ponent)).toMatchSnapshot();
});
});
MyComponent.tsx
import * as React from 'react';
import * as DOM from 'react-dom';
class MyComponent extends React.Component<{}, {}> {
render() {
return <div>{Routes.some_path()}</div>;
}
}
export default MyComponent;
If I console.log(window)
in the test, Routes
does exist. So I'm not sure why it doesn't like Routes
being used int he ponent. In fact it must be Typescript thinking it doesn't exist but at run time it does.
I guess my question then is how can I tell Typescript to chill out about Routes
?
I have a React ponent. For good or bad, I use the Routes
global object exposed by js-routes
- a gem for Rails. I have a snapshot test using Jest which tests my simple ponent that happens to use the Routes
global.
In my test I want to mock Routes
. So when my ponent calls Routes.some_path()
, I can just return some arbitrary string.
The error that I mainly get is Cannot find name 'Routes'.
.
Here is my setup:
package.json
"jest": {
"globals": {
"ts-jest": {
"enableTsDiagnostics": true
}
},
"testEnvironment": "jsdom",
"setupFiles": [
"<rootDir>/app/javascript/__tests__/setupRoutes.ts"
]
...
setupRoutes.ts (This seemed like the most popular solution)
const globalAny:any = global;
globalAny.Routes = {
some_path: () => {},
};
myTest.snapshot.tsx
import * as React from 'react';
import {shallow} from 'enzyme';
import toJson from 'enzyme-to-json';
import MyComponent from 'MyComponent';
describe('My Component', () => {
let ponent;
beforeEach(() => {
ponent = shallow(<MyComponent />);
});
it('should render correctly', () => {
expect(toJson(ponent)).toMatchSnapshot();
});
});
MyComponent.tsx
import * as React from 'react';
import * as DOM from 'react-dom';
class MyComponent extends React.Component<{}, {}> {
render() {
return <div>{Routes.some_path()}</div>;
}
}
export default MyComponent;
If I console.log(window)
in the test, Routes
does exist. So I'm not sure why it doesn't like Routes
being used int he ponent. In fact it must be Typescript thinking it doesn't exist but at run time it does.
I guess my question then is how can I tell Typescript to chill out about Routes
?
1 Answer
Reset to default 8In setupRoutes.ts
(global as any).Routes = {
some_path: () => {}
};
本文标签: javascriptMock a global object with Jest and TypescriptStack Overflow
版权声明:本文标题:javascript - Mock a global object with Jest and Typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741479900a2381104.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论