很简单,我想用Jest运行一个测试.
我放it.only或describe.only但它仍然运行了很多测试.我认为自从我上一次提交以来它运行了所有测试,但它不应该only明确设置标志的这种行为,对吧?
导致此行为的原因以及如何运行单个测试?
我有以下模块我试图在Jest中测试:
// myModule.js
export function otherFn() {
console.log('do something');
}
export function testFn() {
otherFn();
// do other things
}
Run Code Online (Sandbox Code Playgroud)
如上图所示,这部分出口命名的功能和重要的testFn用途otherFn.
在Jest中我正在编写单元测试时testFn,我想模拟该otherFn函数,因为我不希望错误otherFn影响我的单元测试testFn.我的问题是我不确定最好的方法:
// myModule.test.js
jest.unmock('myModule');
import { testFn, otherFn } from 'myModule';
describe('test category', () => {
it('tests something about testFn', () => {
// I want to mock "otherFn" here but can't reassign
// a.k.a. can't do otherFn = jest.fn()
});
});
Run Code Online (Sandbox Code Playgroud)
任何帮助/见解表示赞赏.
我需要测试一个在浏览器中打开新选项卡的函数
openStatementsReport(contactIds) {
window.open(`a_url_${contactIds}`);
}
Run Code Online (Sandbox Code Playgroud)
我想模拟窗口的open函数,这样我就可以验证正确的URL是否传递给open函数.
使用Jest,我不知道如何模仿窗口.我尝试使用模拟函数设置window.open但这种方式不起作用.以下是测试用例
it('correct url is called', () => {
window.open = jest.fn();
statementService.openStatementsReport(111);
expect(window.open).toBeCalled();
});
Run Code Online (Sandbox Code Playgroud)
但它给了我错误
expect(jest.fn())[.not].toBeCalled()
jest.fn() value must be a mock function or spy.
Received:
function: [Function anonymous]
Run Code Online (Sandbox Code Playgroud)
我应该怎么做测试用例?任何建议或提示表示赞赏
我曾经在仅使用 JavaScript 来使用 Jest 时解决了类似的错误,但目前我无法使用 Typescript 来解决类似的错误。
我的所有测试都运行良好,直到我安装了需要@types/jest-environment-puppeteer,@types/puppeteer和的 Puppeteer @types/expect-puppeteer。
安装它们后,puppeteer 测试运行完美,但其他测试开始失败并出现以下错误。
D:\...\api\node_modules\uuid\dist\esm-browser\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
^^^^^^
SyntaxError: Unexpected token 'export'
at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1796:14)
at Object.require (../node_modules/@nestjs/common/decorators/core/injectable.decorator.js:4:16)
Run Code Online (Sandbox Code Playgroud)
我做了什么?
allowJs: true打开tsconfig.json并设置transformIgnorePatternson jest 配置。这样笑话就可以从node_modules/
之后编译文件,此错误停止但测试因另一个奇怪的原因失败。更糟糕的是,测试开始时间增加太多。
所以我保留了allowJs原始设置并更新了笑话配置
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
Run Code Online (Sandbox Code Playgroud)
到
"transform": {
"^.+\\.(t)s$": "ts-jest"
}
Run Code Online (Sandbox Code Playgroud)
所以目前 ts-jest 不编译 js 文件。但我认为我无法让 babel 选择js文件的转换。这些是我的笑话配置:
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用webpack + traceur来调用Ecmascript 6模块来转换为ES5 CommonJS,但是我无法成功地对它们进行单元测试.
我尝试使用Jest + traceur预处理器,但自动插件和依赖名称似乎变得棘手,而且我似乎无法使用sourceMaps来使用Jest和node-inspector调试.
是否有更好的单元测试ES6模块框架?
模拟按钮单击似乎是一个非常简单/标准的操作.然而,我无法在Jest.js测试中使用它.
这是我试过的(也使用jquery),但它似乎没有触发任何东西:
import { mount } from 'enzyme';
page = <MyCoolPage />;
pageMounted = mount(page);
const button = pageMounted.find('#some_button');
expect(button.length).toBe(1); // it finds it alright
button.simulate('click'); // nothing happens
Run Code Online (Sandbox Code Playgroud) 我在 Jest 中遇到以下错误,但不清楚为什么即使添加了 testEnvironmentOptions
TypeError: Cannot read properties of undefined (reading 'testEnvironmentOptions')
at new JSDOMEnvironment (node_modules/jest-environment-jsdom/build/index.js:72:28)
at async TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
at async runJest (node_modules/@jest/core/build/runJest.js:404:19)
Run Code Online (Sandbox Code Playgroud)
这是我的 Jest 配置package.json:
TypeError: Cannot read properties of undefined (reading 'testEnvironmentOptions')
at new JSDOMEnvironment (node_modules/jest-environment-jsdom/build/index.js:72:28)
at async TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
at async runJest (node_modules/@jest/core/build/runJest.js:404:19)
Run Code Online (Sandbox Code Playgroud) 使用 jest-preset-angular 执行单元测试,但收到警告,因为UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON不确定导致错误的原因,因此应用程序卡住并且未运行其他单元测试。
PASS src/app/pages/result/result-filter/result-filter.component.spec.ts (6.251 s)
PASS src/app/pages/result/search-navigation/search-navigation.component.spec.ts
PASS src/app/pages/result/filter-modal/filter-modal.component.spec.ts (5.699 s)
PASS src/app/app.component.spec.ts
PASS src/app/pages/test-type/test-type.component.spec.ts (12.857 s)
(node:3280) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
| property 'element' -> object with constructor 'Object'
| property 'componentProvider' -> object with constructor 'Object'
--- property 'parent' closes the circle
at stringify (<anonymous>)
at writeChannelMessage (internal/child_process/serialization.js:117:20)
at process.target._send (internal/child_process.js:808:17)
at process.target.send (internal/child_process.js:706:19)
at reportSuccess (/Users/macbook/Projects/Playtime Projects/IDP/Idp.Bx.Ui/idp/node_modules/jest-worker/build/workers/processChild.js:67:11) …Run Code Online (Sandbox Code Playgroud) 我有这个行动的反应
export function fetchPosts() {
const request = axios.get(`${WORDPRESS_URL}`);
return {
type: FETCH_POSTS,
payload: request
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下如何测试axios?Jest在那里有这个用例的异步代码,他们使用模拟函数,但我不知道我是否可以用axios做到这一点?参考:https://facebook.github.io/jest/docs/tutorial-async.html
到目前为止,我已经做了这个测试,它正在返回正确的类型
it('should dispatch actions with the correct type', () => {
store.dispatch(fetchPosts());
let action = store.getActions();
expect(action[0].type).toBe(FETCH_POSTS);
});
Run Code Online (Sandbox Code Playgroud)
我不知道如何传递模拟数据并测试它返回但是,有没有人有任何想法?
先感谢您
我在这个测试中遇到了一个奇怪的问题:
import Deal from "../src/models/Deal";
import apiProducts from "../__mocks__/api/products";
describe("Deal", () => {
describe("Deal.fromApi", () => {
it("takes an api product and returns a Deal", () => {
const apiDeal = apiProducts[0];
const newDeal = Deal.fromApi(apiDeal);
const expected = expectedDeal();
expect(newDeal).toEqual(expected);
});
});
});
Run Code Online (Sandbox Code Playgroud)
export default class Deal {
// no constructor since we only ever create a deal from Deal.fromApi
static fromApi(obj: Object): Deal {
const deal = new Deal();
deal.id = obj.id;
deal.name = obj.name;
deal.slug …Run Code Online (Sandbox Code Playgroud) jestjs ×10
javascript ×6
unit-testing ×5
ecmascript-6 ×2
typescript ×2
angular ×1
babeljs ×1
enzyme ×1
mocking ×1
nest ×1
node.js ×1
puppeteer ×1
react-redux ×1
reactjs ×1
testing ×1
traceur ×1