-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError: Cannot read property 'resolve' of undefined #3
Comments
I looked through my local jest-mock-axios package but since it's transpiled I didn't get much out of it. Looking at the source on github, it becomes much more clear. The error in the stack trace points to lib/mock-axios.ts#L66. What is not clear is from where the response is suppose to come from. Since we are mocking an HTTP call, I expect that non existing end-points can be used for testing. |
OK, I was too fast. Of couse the |
Hmm.. The README says that What am I missing? |
I've never written typescript so I might be wrong but let's assume that the type Node: v8.9.4 |
Jesus. I misspelled |
I'm having this same issue but have the Even though I'm passing in a promise this error throws, which is unexpected. I am using TypeScript and this is a sample of the code I'm working with: // currency.ts
import axios from 'axios'
const CURRENCY_API = '/v1/currency'
class Currency {
static price() {
return axios
.get(CURRENCY_API)
.then((resp: axios.Response) => Number(resp.data[0].price_usd))
.catch((error: Error) => console.log(error))
}
}
export default Currency // currency.test.ts
import mockAxios from 'jest-mock-axios'
import Currency from './currency'
describe('server/models/currency', () => {
afterEach(() => mockAxios.reset())
describe('.price', () => {
test('returns the correct currency valule', async () => {
const resp = { data: [{ price_usd: '15000' }] }
mockAxios.mockResponse(resp)
const actual = await Currency.price()
expect(mockAxios.get).toHaveBeenCalled()
const expected = 15000
expect(actual).toBe(expected)
})
})
}) |
@knee-cola any chance you have some ideas on why this would be happening? |
I ran into the same problem today. |
I ended up just using direct mocking using Jest that works pretty well: import axios from 'axios'
import http from './http'
jest.mock('axios')
test('should return data', async () => {
const url = '/foo'
const resp = { data: 'bar' }
axios.get.mockReturnValue(resp)
const data = await http.get(url)
expect(axios.get).toBeCalledWith(url)
expect(data).toBe(resp.data)
}) |
I have managed to reproduce the original error reported by @dotnetCarpenter and can confirm he's findings . This problem can be caused by mispelling the "mocks" directory. The mocking file needs to ne placed inside the mocks directory - not the *mock dir. @danawoodman have you configured Jest to transform TypeScript before running the tests? Here's a sampe module.exports = {
"verbose": true,
"testRegex": "/test/.*\\.spec\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
}
}; Also here's the {
// docs: https://basarat.gitbooks.io/typescript/content/docs/project/tsconfig.html
"compilerOptions": {
"noImplicitAny": true,
// module resolution strategy ('node','classic')
"moduleResolution": "node",
// ECMAScript target version ('ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT')
"target": "ESNEXT",
// module code generation ('commonjs', 'amd', 'system', 'umd' or 'es2015')
"module": "commonjs",
// create declaration files in output folder
"declaration": true,
"declarationDir": "./"
},
// excluding folders from being included
// while creating `declaration` files
"exclude": [
"test",
"__mocks__"
]
} |
@knee-cola yes it is transforming TypeScript |
I'm seeing the same error as described by OP. The dir is correctly spelled as mocks.
|
I ran across this same issue and thought I would comment for anyone else that may come across this issue. If the problem isn't the spelling/placement of the If it comes before the axios call is triggered then you will get this error. So trigger the call, then tell mockAxios how you want it to respond. |
@timthompson nice catch, that was pretty non-obvious :) |
I need help to understand this error message. The stack trace is:
I've followed the README and created a
__mock__/axios.js
file in the root.I then created a simple test case in
tests/loader.test.js
:In `tests/tmp/dummy.js' I have:
The result is:
The text was updated successfully, but these errors were encountered: