jestjs

How to test useRef with the “current” prop in jest/enzyme

别说谁变了你拦得住时间么 提交于 2021-02-06 20:09:01
问题 I hope someone can point me in the right direction to test useRef in the component below. I have a component structured something like below. I am trying to test the functionality within the otherFunction() but I'm not sure how to mock the current property that comes off the component ref. Has anyone done something like this before? const Component = (props) => { const thisComponent = useRef(null); const otherFunction = ({ current, previousSibling }) => { if (previousSibling) return

How to test useRef with the “current” prop in jest/enzyme

為{幸葍}努か 提交于 2021-02-06 19:56:28
问题 I hope someone can point me in the right direction to test useRef in the component below. I have a component structured something like below. I am trying to test the functionality within the otherFunction() but I'm not sure how to mock the current property that comes off the component ref. Has anyone done something like this before? const Component = (props) => { const thisComponent = useRef(null); const otherFunction = ({ current, previousSibling }) => { if (previousSibling) return

How to unit testing with jest in vue composition api component?

假装没事ソ 提交于 2021-02-06 10:13:04
问题 I'm writing a unit test with jest, for my composition API component in vue.js. But I can't access to functions in composition API's setup(). Indicator.vue <template> <div class="d-flex flex-column justify-content-center align-content-center"> <ul class="indicator-menu d-flex justify-content-center"> <li v-for="step in steps" :key="step"> <a href="#" @click="updateValue(step)" :class="activeClass(step, current)"> </a> </li> </ul> <div class="indicator-caption d-flex justify-content-center">

How to unit testing with jest in vue composition api component?

有些话、适合烂在心里 提交于 2021-02-06 10:11:45
问题 I'm writing a unit test with jest, for my composition API component in vue.js. But I can't access to functions in composition API's setup(). Indicator.vue <template> <div class="d-flex flex-column justify-content-center align-content-center"> <ul class="indicator-menu d-flex justify-content-center"> <li v-for="step in steps" :key="step"> <a href="#" @click="updateValue(step)" :class="activeClass(step, current)"> </a> </li> </ul> <div class="indicator-caption d-flex justify-content-center">

How to unit testing with jest in vue composition api component?

别说谁变了你拦得住时间么 提交于 2021-02-06 10:09:54
问题 I'm writing a unit test with jest, for my composition API component in vue.js. But I can't access to functions in composition API's setup(). Indicator.vue <template> <div class="d-flex flex-column justify-content-center align-content-center"> <ul class="indicator-menu d-flex justify-content-center"> <li v-for="step in steps" :key="step"> <a href="#" @click="updateValue(step)" :class="activeClass(step, current)"> </a> </li> </ul> <div class="indicator-caption d-flex justify-content-center">

How to mock/replace getter function of object with Jest?

浪尽此生 提交于 2021-02-05 20:40:11
问题 In Sinon I can do the following: var myObj = { prop: 'foo' }; sinon.stub(myObj, 'prop').get(function getterFn() { return 'bar'; }); myObj.prop; // 'bar' But how can I do the same with Jest? I can't just overwrite the function with something like jest.fn() , because it won't replace the getter "can't set the value of get" 回答1: You could use Object.defineProperty Object.defineProperty(myObj, 'prop', { get: jest.fn(() => 'bar'), set: jest.fn() }); 回答2: For anyone else stumbling across this

Why does Jest throw an error when it should not?

纵然是瞬间 提交于 2021-02-05 12:27:26
问题 I have a function like this: const test = (match) => { if (match !== 'cat') { throw new TypeError() } } My test would be: describe('test', () => { it('must throw error'() => { try { test('cat') catch(err){ expect(err).toStrictEqual(new TypeError()) } } } But the test passed. It should fail. Why did it pass? 回答1: Tests only fail if there is an unmet expectation. In your case, because no error is thrown, no expectation is ever even evaluated. Therefore the test passes. To deal with this, either

Why does Jest throw an error when it should not?

╄→尐↘猪︶ㄣ 提交于 2021-02-05 12:25:01
问题 I have a function like this: const test = (match) => { if (match !== 'cat') { throw new TypeError() } } My test would be: describe('test', () => { it('must throw error'() => { try { test('cat') catch(err){ expect(err).toStrictEqual(new TypeError()) } } } But the test passed. It should fail. Why did it pass? 回答1: Tests only fail if there is an unmet expectation. In your case, because no error is thrown, no expectation is ever even evaluated. Therefore the test passes. To deal with this, either

What's the best way to test express.js API

余生颓废 提交于 2021-02-05 11:57:47
问题 I'm new in API testing with JavaScript. I've found many solution for testing REST APIs, but not sure what's the best. Im using express.js in the backend and for the tests jest. I've seen that I can test with jest, by mocking the function or that I can also mock the API directly. I've a data.js where the object is stored: let data = [ { id: 0, name: "empty shopppinglist", location: "", targetDate: "", priority: "", isFinished: false, items: ["vodka" ] } ] module.exports = data Then in the

Create React App doesn't properly mock modules from __mocks__ directory

空扰寡人 提交于 2021-02-05 11:36:22
问题 I have a working example with Jest and mocks from __mocks__ directory that works : With simple Jest setup // package.json { "name": "a", "version": "1.0.0", "main": "index.js", "scripts": { "test": "jest" }, ... "devDependencies": { "jest": "^26.6.3" }, "dependencies": { "@octokit/rest": "^18.0.12" } } And then /index.js : const { Octokit } = require("@octokit/rest"); const octokit = new Octokit(); module.exports.foo = function() { return octokit.repos.listForOrg({ org: "octokit", type: