jestjs

jest: How to teardown after (just) an individual test

不羁岁月 提交于 2021-01-28 01:33:18
问题 jest provides afterEach , beforeEach , afterAll and beforeAll to complete setup and teardown logic. What I would like to do, is to clear up after one particular test. Consider the following: describe("a family of tests it makes sense to group together", () => { ... test("something I want to test", () => { // some setup needed for just this test global.foo = "bar" // the test expect(myTest()).toBe(true) // clear up delete global.foo } ... } The problem with the above... If the test above fails

Testing a Redux action that dispatches 2 other functions with Jest?

烂漫一生 提交于 2021-01-28 01:05:39
问题 I have a function (a Redux action) which calls 2 functions. I can't figure how how to test this with Jest: This is my function: import doSomething from 'redux/do-something'; import doSomethingElse from 'redux/do-something-else'; export default () => async dispatch => { await dispatch(doSomething()); dispatch(doSomethingElse()); }; This is my test: import doSomething from 'redux/do-something'; import doSomethingElse from 'redux/do-something-else'; import functionToTest from 'redux/function-to

Unable to mock API request

喜夏-厌秋 提交于 2021-01-28 00:40:08
问题 Having a really hard time setting up jest-test-mock in in my TypeScript project. I was wondering if someone could point me in the right direction? I've got a function that looks like this: retrieveData.ts import fetch from 'cross-fetch'; export async function retrieveData({ endpoint, configuration, auth }: dataInterface): Promise<object> { try { console.log('Fetching the requested data... 📦') const settings = configuration ? JSON.parse(render(configuration || '', auth)) : {} if (settings.body

detox jest tests are not running in parallel

安稳与你 提交于 2021-01-28 00:25:32
问题 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

React Native app - crash/close on startup on Android 8 real device

江枫思渺然 提交于 2021-01-27 21:20:59
问题 I have a React Native project (not Expo) initialized with react native init myapp . I can run this application on Emulator and Real Device with Android 5, but cannot run the app on my Android Mobile with Android 8. Here's the package.json : { "name": "myapp", "version": "2.0.0", "private": true, "main": "lib/index.js", "scripts": { "start": "node node_modules/react-native/local-cli/cli.js start", "test": "jest" }, "dependencies": { "axios": "^0.18.0", "formik": "^1.5.1", "iso-639-1": "^2.0.3"

Jest: How to get arguments passed to mock constructor?

落花浮王杯 提交于 2021-01-27 18:09:51
问题 If I want to create a mock implementation of an instance method of an ES6 Class I would do this // ExampleClass.js export class ExampleClass { constructor(someValue) { this.a = someValue; } exampleMethod(anotherValue) { // do something with 'anotherValue' } } // OtherModule.js import {ExampleClass} from './ExampleClass'; export const fooBar = () => { const ex = new ExampleClass("hello world"); ex.exampleMethod("another value"); }; // ExampleClass.test.js import {fooBar} from './OtherModule';

Jest: How to get arguments passed to mock constructor?

江枫思渺然 提交于 2021-01-27 18:00:59
问题 If I want to create a mock implementation of an instance method of an ES6 Class I would do this // ExampleClass.js export class ExampleClass { constructor(someValue) { this.a = someValue; } exampleMethod(anotherValue) { // do something with 'anotherValue' } } // OtherModule.js import {ExampleClass} from './ExampleClass'; export const fooBar = () => { const ex = new ExampleClass("hello world"); ex.exampleMethod("another value"); }; // ExampleClass.test.js import {fooBar} from './OtherModule';

testing chrome.storage.local.set with jest

旧巷老猫 提交于 2021-01-27 17:54:36
问题 I'm a very beginner with testing and started using jest. This function I have saves data in chrome.storage.local and I want to test if chrome.storage.local called 3 times. Here is the function: saveData: function(){ chrome.storage.local.set({'blackList': bgModule.blackList}, function() {}); chrome.storage.local.set({'pastDays': bgModule.pastDays}, function() {}); chrome.storage.local.set({'websiteList': bgModule.websiteList}, function() {}); } and here is my test: const bgModule = require("..

Jest unit tests in Visual Studio 2019 Test Explorer

笑着哭i 提交于 2021-01-27 13:03:05
问题 I created a new ASP.NET Core React app using dotnet new react . Then I added some Jest unit tests, which run nicely when I run npm test on the command line. I would like to be able to run the tests from Visual Studio 2019, either using the Test Explorer window or ReSharper. First of all, it seems that ReSharper only supports Jasmine and not Jest (ReSharper documentation, Feature request). So I tried using the Test Explorer, following this official guide. It has support for Jest. However, the

Trying to make a simple Axios mock with jest

醉酒当歌 提交于 2021-01-27 07:33:11
问题 I am trying to understand this example, so the first thing I am trying is removing his axiosConfig.js , so the example looks more like the current case I would like to solve. However I get this error - Expected + Received Object { + "baseURL": "https://jsonplaceholder.typicode.com/albums", "method": "get", "url": "/3/photos?_limit=3", }, Number of calls: 1 39 | const photos = await getPhotosByAlbumID(3); 40 | expect(axios.request).toHaveBeenCalled(); > 41 | expect(axios.request)