jestjs

Jest test not reading environment variables with dotenv

我们两清 提交于 2021-01-28 05:41:15
问题 I'm running a test on a function that calls for environment variables, I'm getting undefined. Solutions I tried and didn't work: 1/ add require('dotenv').config({path:'../.env'}) in my test file 2/ pass globals in package.json "jest": { "globals": { "USER_ENDPOINT":"xxx", "USER_KEY":"xxx" } } 3/ pass my variables in test command in package.json "test": "USER_ENDPOINT:xxx USER_KEY:xxx jest --watchAll --detectOpenHandles" 4/ added an Object.assign in a beforeEach() in my test file beforeEach(()

Jest reports that test passes even though expect assertion fails

与世无争的帅哥 提交于 2021-01-28 04:45:09
问题 In this code sandbox the following test is executed: import { of } from "rxjs"; import { map } from "rxjs/operators"; const source = of("World"); test("It should fail", () => { source.subscribe(x => expect(x).toContain("NOTHING")); }); Jest reports it as passing, even though the expectation fails. Thoughts? 回答1: rxjs observables are asynchronous. Take a look at this page to help with testing asynchronous code You want to add the done argument as below -> test("It should fail", done => {

Jest reports that test passes even though expect assertion fails

ぃ、小莉子 提交于 2021-01-28 04:33:29
问题 In this code sandbox the following test is executed: import { of } from "rxjs"; import { map } from "rxjs/operators"; const source = of("World"); test("It should fail", () => { source.subscribe(x => expect(x).toContain("NOTHING")); }); Jest reports it as passing, even though the expectation fails. Thoughts? 回答1: rxjs observables are asynchronous. Take a look at this page to help with testing asynchronous code You want to add the done argument as below -> test("It should fail", done => {

How do I share setup and teardown in jest tests?

你。 提交于 2021-01-28 04:31:58
问题 I've found react tests recipes quite verbose, because they need to setup a container and cleanup that after each test. I would like to share that setup-teardown code between test files that require that but could not find a way to do it and I would prefer not to repeat beforeEach and afterEach on every component test. I've tried with something like: # sample.test.js #... import container from './react_helpers.js' #... # react_helpers.js import { unmountComponentAtNode } from "react-dom" let

How to test an async function in an event listener with Jest?

ぃ、小莉子 提交于 2021-01-28 04:26:29
问题 I have an event listener that runs an asynchronous function, and removes some elements from the DOM after completion: async function fetchAndRemove() { try { const response = await fetch('/endpoint-that-returns-json') const result = await response.json() if (result.status === 'Success') { document.querySelector('.my-element').remove() } } catch (error) { console.log(error) } } function setupListeners () { const button = document.querySelector('.my-button') button.addEventListener('click', ()

detox jest tests are not running in parallel

ⅰ亾dé卋堺 提交于 2021-01-28 03:11:51
问题 Detox Automation with Jest Hi, I am running detox tests form the CLI with the below command. detox test -r --maxWorkers 2 to achieve parallel execution. Already one simulator started with name iphone 5s-Detox and with the maxWorkers 2 or --workers 2, one more simulator is launching but the tests are executing in only one simulator. I want to execute tests in parallel to save the execution time. Can anyone share concept of detox jest parallel execution. Is this same as testNG or it's different

Enzyme instance() returns null

寵の児 提交于 2021-01-28 03:03:58
问题 I have the following test: describe('Form', () => { let store; let wrapper; beforeEach(() => { store = mockStore(mockData); wrapper = mount( <Provider store={store}> <Form /> </Provider> ); }); it('handleForm calls uses validate() for validation', () => { const instance = wrapper.instance(); const submitFormButton = wrapper.find('.submitFormButton'); submitFormButton.simulate('click'); console.log(instance); // null }); }); Any idea of what I'm doing wrong exactly? I know that Enzyme has this

Mock node external module default with chained method using jest

丶灬走出姿态 提交于 2021-01-28 02:54:31
问题 In our node CLI we have a simple method: 'use strict'; const ora = require('ora'); module.exports = function startSpinner({ textOnStart, color, spinnerType }) { const spinner = ora({ text: textOnStart, color: color || 'cyan', spinner: spinnerType || '' }).start(); }; We try to use jest to test this method. We have two tests to achieve: Testing that ora has been called with proper object argument Testing that the method start() was called afterward That being said we cannot achieve to mock ora

Testing asynchronous componentDidMount that changes state with Jest and Enzyme

元气小坏坏 提交于 2021-01-28 02:05:35
问题 All I am doing in my code is upon componentDidMount being run, I am making an axios get request to GitHub, and setting some data back onto state. However, when I run the test it still says the state is an empty array. Here is my component below: export default class HelloWorld extends Component { constructor(props) { super(props) this.state = { goodbye: false, data: [] } } async componentDidMount() { await this.func() } func = async () => { let data = await axios.get('https://api.github.com

how to jest test an async action with axios in react?

心不动则不痛 提交于 2021-01-28 01:41:14
问题 I have an action-generator register.js: import { REGISTER_SUCCESS, REGISTER_FAIL } from "./types"; import axios from "axios"; export const register = (formData) => async (dispatch) => { const { name, email, password } = formData; const configRegister = { method: "post", url: "/api/users", headers: { "Content-Type": "application/json" }, data: { name, email, password }, }; try { const res = await axios(configRegister); const token = res.data.token; dispatch({ type: REGISTER_SUCCESS, payload: {