我是Jest的新手,我正在尝试运行一个简单的单元测试,以确保一切都设置正确,并且在编译期间遇到了许多问题来解决错误。
当运行测试套件时,Jest成功地找到了我试图测试的文件,并在第1行生成了以下意外的标识符错误消息。是不是少了点什么?我已经尝试解决这个问题很长一段时间了。
/Users/foo/Sites/test/Test.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Test from
我刚刚开始使用Jest测试框架,虽然直接的单元测试工作良好,但我在测试其模块(通过babel+webpack的ES模块)中需要HTML文件的任何组件时都遇到了大量问题。
下面是一个示例:
import './errorHandler.scss';
import template from './errorHandler.tmpl';
class ErrorHandler {
...
我正在加载特定于组件的SCSS文件,它是我在Jest的package.json配置中设置的,以返回一个空对象,但是当Jest尝试运行import template from
我在这里看到过关于嘲笑默认出口的问题,但我不认为这已经被问到了:
在模拟正在测试的模块的依赖项的默认导出时,如果模块使用ES6 import语句导入依赖项,则测试套件无法运行,但是,如果模块使用require().default调用,则声明TypeError: (0 , _dependency.default) is not a function It succeeds。
在我的理解中,import module from location直接翻译成const module = require(location).default,所以我很困惑为什么会发生这种情况。我宁愿保持代码风格的一致性,而
在运行npm测试时,我会得到以下错误消息:我不知道要解决什么和如何解决。
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modul
我有一个非常简单的测试:
describe('sanity', () => {
it('sanity', () => {
expect(true).toBeTruthy()
})
})
我收到了以下错误:
FAIL spec/javascript/sanity_test.js
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a fi
我正在尝试使用类型记录中的request()来模拟节点模块request的jest函数,但是我做不到,有人能帮我哪里出了问题吗?顺便说一句,我正在尝试创建一个通用的代理函数,它应该与所有的http方法一起工作,比如get、post、delete、update。因此,我只想使用request()函数,而不是使用request.get(),request.post()...etc,使用基于request方法的if- method梯子。
Proxy.ts:
import * as request from 'request';
export default class ProxyC
是否可以仅为构建排除所有测试文件,但使用它们与nodemon一起在本地运行测试?当我在tsconfig.json文件中排除测试文件时,我会得到一个类型记录错误,它找不到测试库的类型,就像我的例子中的jest一样。
Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.ts(2582)
{
"compilerOptions": {},
测试套件一直运行得很好,直到没有。奇怪的事情只发生在我的机器上,CI工作正常。
我们的项目使用了Typecript,React,Jest和酵素,这就是我运行yarn jest时得到的
Test suite failed to run
ReferenceError: enzyme_1 is not defined
6 | // (global as any).jQuery = $
7 |
> 8 | Enzyme.configure({ adapter: new Adapter() })
| ^
9 |
at Object.<anonymous>
我使用的jest:24.9.0没有任何配置,从一个创建-反应-应用程序全球安装。在这些文件中,我使用es6模块。使用"test": "react-scripts test"时没有错误
但是,当我将jest与"test": "jest --config jest.config.js",一起使用时,我会看到下面的错误。
FAIL src/configuration/notifications.test.js
● Test suite failed to run
/var/www/management/node/src
我试图使用Jest手动模拟来模拟项目中使用的包X的行为。
实际应用程序代码中包X的使用如下所示:
// Real.js
import X from '../lib/X.js';
export default class App {
...
execute() {
const x = new X(); // creating a new instance of X
x.doSomething(); // calling someThing() of X
}
...
}
我的Jest手动模拟以模仿X的行为,如下所示:
global
我正在尝试用Jest测试一个承诺,当它在浏览器中运行时,这段代码会按照它应该执行的方式执行。不过,我想开始为它编写测试。
class ListCollection {
constructor() {
this.items = new Array();
}
addItem(string) {
const addItemPromise = new Promise(
function (resolve, reject) {
// set up async getting like a XMLHttpRequest
setTi
我正在编写一个Jest集成测试来测试我的服务器。这是我的测试:
import { app, port } from '../server' // the other lines are not so important
...
当运行jest时,会引发一个错误:
__tests__/graphql.test.js
● Test suite failed to run
<project folder>/node_modules/papercut/lib/papercut.coffee:3
{FileStore, S3Store, TestStor
一段时间以来,我一直试图模拟fetchLiveMatches导入的函数,但没有成功。我一直在浏览一些想法,但我想我已经用完了,所以我需要一些帮助。你知道我做错了什么吗?
live.test.js
import * as liveController from "./live";
import { jest } from "@jest/globals";
import * as liveService from "../service/live";
import { buildReq, buildRes, buildNext } from "
我有一个Jest测试套件无法运行,因为它试图测试的组件依赖于RequireJS模块。下面是我看到的错误:
FAIL __tests__/components/MyComponent.test.js
● Test suite failed to run
ReferenceError: define is not defined
at Object.<anonymous> (node_modules/private-npm-module/utils.js:1:90)
该组件具有以下导入:
import utils from 'private-np
我已经在typescript中使用ts-jest对我的React-app进行了测试。 import * as React from "react";
import * as renderer from "react-test-renderer";
import { ChartTitle } from "Components/chart_title";
describe("Component: ChartTitle", () => {
it("will be rendered with no error
我在一个react代码库中添加了类型记录支持,当应用程序正常工作时,jest测试在整个地方都失败了,显然没有认识到一些关于es6语法的东西。
我们为此使用了。下面是我得到的错误消息,在尝试处理jest的测试设置文件时马上就收到了。
FAIL src/data/reducers/reducers.test.js
● Test suite failed to run
/Users/ernesto/code/app/react/setupTests.js:1
({"Object.<anonymous>":function(module,expor