Skip to content
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

Issues with Create-React-App #1

Closed
HoltMansfield opened this issue Mar 2, 2018 · 18 comments
Closed

Issues with Create-React-App #1

HoltMansfield opened this issue Mar 2, 2018 · 18 comments

Comments

@HoltMansfield
Copy link

TypeError: jest.requireActual is not a function

at td.mock (node_modules/testdouble-jest/index.js:13:28)

The instance of jest that gets passed to testdouble has the following signature:

{
addMatchers: [Function: addMatchers],
autoMockOff: [Function: disableAutomock],
autoMockOn: [Function: enableAutomock],
clearAllMocks: [Function: clearAllMocks],
clearAllTimers: [Function: clearAllTimers],
deepUnmock: [Function: deepUnmock],
disableAutomock: [Function: disableAutomock],
doMock: [Function: mock],
dontMock: [Function: unmock],
enableAutomock: [Function: enableAutomock],
fn: [Function: bound fn],
genMockFn: [Function: bound fn],
genMockFromModule: [Function: genMockFromModule],
genMockFunction: [Function: bound fn],
isMockFunction: [Function: isMockFunction],
mock: [Function: mock],
resetAllMocks: [Function: resetAllMocks],
resetModuleRegistry: [Function: resetModules],
resetModules: [Function: resetModules],
runAllImmediates: [Function: runAllImmediates],
runAllTicks: [Function: runAllTicks],
runAllTimers: [Function: runAllTimers],
runOnlyPendingTimers: [Function: runOnlyPendingTimers],
runTimersToTime: [Function: runTimersToTime],
setMock: [Function: setMock],
spyOn: [Function: bound spyOn],
unmock: [Function: unmock],
useFakeTimers: [Function: useFakeTimers],
useRealTimers: [Function: useRealTimers]
}

Steps to reproduce:

Create a react app with create-react-app

Install testdouble-jest and configure as described in the docs.

Try to use replace as:

let bar, subject
describe('td.replace', () => {
beforeEach(() => {
bar = td.replace('./bar')
subject = require('./foo')
})

I'd be interested in helping out with this but it almost seems like there would need to be a whole separate library for this. Something like testdouble-jest-for-cra

@searls
Copy link
Member

searls commented Mar 2, 2018

As a non-CRA, non-Jest user can you help me understand a bit why Jest is different under CRA than independently?

@davemo
Copy link

davemo commented Mar 2, 2018

Hi @HoltMansfield, could you post your test helper file where you are including testdouble-jest so we can see the invocation context?

@HoltMansfield
Copy link
Author

@davemo my pleasure

/* eslint import/no-extraneous-dependencies: "off" */
import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import {createSerializer} from 'enzyme-to-json'

import mockLocalStorage from '../test/mockLocalStorage'

// setup enzyme to work with React 16
configure({ adapter: new Adapter() })

// setup enzyme as teh default serializer
expect.addSnapshotSerializer(createSerializer({mode: 'deep'}))

// mock local localStorage
mockLocalStorage()

global.td = require('testdouble')
require('testdouble-jest')(td, jest)

@HoltMansfield
Copy link
Author

Here is what all gets installed in node_modules from CRA

image

@HoltMansfield
Copy link
Author

requireActual is not actually inside the jest package

@davemo
Copy link

davemo commented Mar 2, 2018

Looks to me like you haven't imported jest anywhere in your helper, so in effect it seems like require('testdouble-jest')(td, jest) is passing jest as undefined.

@HoltMansfield
Copy link
Author

It's definitely not undefined. When I console.log it I get the output from my original post. Let me try again to explicitly import jest. I did that earlier today but it was in the middle of trying a million things.

@HoltMansfield
Copy link
Author

HoltMansfield commented Mar 2, 2018

`/* eslint import/no-extraneous-dependencies: "off" */
import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import {createSerializer} from 'enzyme-to-json'
import jest from 'jest'

// setup enzyme to work with React 16
configure({ adapter: new Adapter() })

// setup enzyme as teh default serializer
expect.addSnapshotSerializer(createSerializer({mode: 'deep'}))

global.td = require('testdouble')
require('testdouble-jest')(td, jest)`

Results in a different error:

TypeError: jest.mock is not a function

  at td.mock (node_modules/testdouble-jest/index.js:11:30)
  at jestModule (node_modules/testdouble/lib/replace/jest-module.js:19:5)
  at Object.<anonymous>.exports.default (node_modules/testdouble/lib/replace/module.js:10:109)
  at Object.<anonymous>.exports.default [as replace] (node_modules/testdouble/lib/replace/index.js:9:29

I'm extremely appreciative of your time gentlemen. I sincerely hope I'm not just doing something stupid. :)

@HoltMansfield
Copy link
Author

requireActual isn't in any of the jest packages...seems to get tacked on by require?

@searls
Copy link
Member

searls commented Mar 2, 2018

git-grep pointed me to the fact jest.requireActual was added in August 2017 via this PR jestjs/jest#4260

@searls
Copy link
Member

searls commented Mar 2, 2018

(I haven't looked into this further yet)

@HoltMansfield
Copy link
Author

HoltMansfield commented Mar 2, 2018

@searls I'm sorry I didn't point out earlier that CRA is behind on jest. They're on 20.0.4

I installed the latest jest and everything blows up so it seems it doesn't work with CRA.

This may be yet another reason to make my own webpack config.

@searls
Copy link
Member

searls commented Mar 2, 2018

The people who maintain CRA should talk to the people who maintain Jest!

@searls
Copy link
Member

searls commented Mar 2, 2018

@davemo the reason jest is not undefined is because Jest runs each test in a vm and assigns jest to the nebulous module scope which effectively makes it a global from the perspective of everything invoked by the test.

I'm suddenly seeing an entire weekend spent on this flash before my eyes.

@HoltMansfield
Copy link
Author

@davemo please hit me up if I can help. I love using testdouble. I'm available all weekend.

@searls
Copy link
Member

searls commented Mar 3, 2018

According to this response, CRA should be out in a few weeks and (at least somebody) expects it to remedy this situation https://twitter.com/lgandecki/status/969877478335500288

requireActual & requireMock were quite necessary to getting testdouble-jest to work, so I'd advise you to upgrade or to lobby for a CRA upgrade path (one hopes there is one planned)

@searls searls closed this as completed Mar 3, 2018
@venikx
Copy link

venikx commented Aug 28, 2018

I tested it and yes, test-double seems to work on the CRA@next branch. However the CRA@next branch is rather unstable and is currently broken due to something related to Babel.
It was fun though, working with testdouble ^^

@HoltMansfield
Copy link
Author

Awesome! Thanks @AnaRobynn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants