jestjs

Unexpected token import in custom node environment

て烟熏妆下的殇ゞ 提交于 2021-02-08 03:13:33
问题 I am getting a SyntaxError: Unexpected token import when using my custom class that extends NodeEnvironment . When not using a custom test environment, modern JavaScript - ES modules, etc. works fine. I'm using node 8.11.1 and yarn 1.6.0 . This is the full error: $ jest --no-cache FAIL __tests__/math.js ● Test suite failed to run /home/user/workspace/web/jest-custom-environment-no-import/__tests__/_helpers_/environments/integration.js:1 (function (exports, require, module, __filename, _

Unexpected token import in custom node environment

大兔子大兔子 提交于 2021-02-08 03:13:28
问题 I am getting a SyntaxError: Unexpected token import when using my custom class that extends NodeEnvironment . When not using a custom test environment, modern JavaScript - ES modules, etc. works fine. I'm using node 8.11.1 and yarn 1.6.0 . This is the full error: $ jest --no-cache FAIL __tests__/math.js ● Test suite failed to run /home/user/workspace/web/jest-custom-environment-no-import/__tests__/_helpers_/environments/integration.js:1 (function (exports, require, module, __filename, _

Unexpected token import in custom node environment

烈酒焚心 提交于 2021-02-08 03:10:21
问题 I am getting a SyntaxError: Unexpected token import when using my custom class that extends NodeEnvironment . When not using a custom test environment, modern JavaScript - ES modules, etc. works fine. I'm using node 8.11.1 and yarn 1.6.0 . This is the full error: $ jest --no-cache FAIL __tests__/math.js ● Test suite failed to run /home/user/workspace/web/jest-custom-environment-no-import/__tests__/_helpers_/environments/integration.js:1 (function (exports, require, module, __filename, _

How to test for the existance of a bootstrap vue component in unit tests with jest?

孤者浪人 提交于 2021-02-08 02:36:12
问题 So I have some code that has a b-form-input component and I am testing whether that component renders. I am using wrapper.find({name: "b-form-input"}).exists() to determine whether that bootstrap vue component exists. However this function continually returns false when I know that the component is rendering. Could I have some help on how to do this correctly? 回答1: Looking at the bootstrap-vue source code, it looks like the name of the element is BFormInput and not b-form-input (it was

Exports defaults else path not taken with Jest and ES6

元气小坏坏 提交于 2021-02-07 19:22:22
问题 I'm having issues to pass my tests with 100% coverage. Istanbul say that exports defaults Component else path not taken. Because of that, I see in my generated html of istanbul that my tests are not completely at 100%. Mostly in the Statements and Branches tab. I'm using: React: 15.4.0 Jest: 17.0.2 Webpack: 1.12.11 Any idea? 回答1: The problem was in the jest configuration, we were using a preprocessor in order to resolve some imports: In the package json we had this: "transform": { "^.+\\.js$"

TypeScript doesn't recognize my jest mock module

我只是一个虾纸丫 提交于 2021-02-07 13:36:55
问题 Suppose I have an index.ts that will import Database.ts and run some queries. To test this index.ts file, I want to mock the Database.ts because I don't want to connect to any real database. This is my index.ts : import { connect } from './Database' export async function runSomeQuery() { const connection = await connect() const result = await connection.query('SOME QUERY') return result } and here is the mock of database ( __mocks__/Database.ts ) const mockConnection = { query: jest.fn() }

TypeScript doesn't recognize my jest mock module

南笙酒味 提交于 2021-02-07 13:32:34
问题 Suppose I have an index.ts that will import Database.ts and run some queries. To test this index.ts file, I want to mock the Database.ts because I don't want to connect to any real database. This is my index.ts : import { connect } from './Database' export async function runSomeQuery() { const connection = await connect() const result = await connection.query('SOME QUERY') return result } and here is the mock of database ( __mocks__/Database.ts ) const mockConnection = { query: jest.fn() }

How to get a Jest custom matcher working in typescript?

旧时模样 提交于 2021-02-07 11:49:19
问题 I regularly have unit tests where I need to compare two moment objects. I'd us moment's built-in function moment.isSame(moment) to compare them. However, this means my assertion will look like this: expect(moment1.isSame(moment2)).toBeTrue(); I didn't quite like this, especially because the failure message will be less informative. Hence, I wanted to write a custom jest matcher "toBeSameMoment". The following code seems to compile at least: import moment from "moment"; declare global {

How to get a Jest custom matcher working in typescript?

烈酒焚心 提交于 2021-02-07 11:48:43
问题 I regularly have unit tests where I need to compare two moment objects. I'd us moment's built-in function moment.isSame(moment) to compare them. However, this means my assertion will look like this: expect(moment1.isSame(moment2)).toBeTrue(); I didn't quite like this, especially because the failure message will be less informative. Hence, I wanted to write a custom jest matcher "toBeSameMoment". The following code seems to compile at least: import moment from "moment"; declare global {

Use Jest to test secure cookie value

烈酒焚心 提交于 2021-02-07 08:28:36
问题 Previously I had Jest code to test for the value of a non-secure cookie. It looked like this: expect(Cookie.get('myToken')).toBe('tokenvalue'); (the Cookie in this instance is using the js-cookie api) However, I tried to update how I set my cookie, and added the secure flag and set it to true . Jest no longer can see this cookie when the test runs. What's the best way to test it? I've looked at questions like Jest secure cookies? I've also tested the code and checked in a browser that the