Closed richiethomas closed 3 years ago
Thanks for the detailed write-up. We don't get a lot of feedback on the React tutorial, so this is very helpful.
If I understand correctly, it looks like there were two main things that went wrong:
I've updated the tutorial to include npm commands as well as yarn and to point out the RTL documentation issue. Could you take a look and let me know if the updated doc would've worked better for you?
One other thing: The direct equivalent of yarn run jasmine
is either npx jasmine
or ./node_modules/.bin/jasmine
(the two differ mainly in what they do when the package isn't there). If plain jasmine init
works, that might mean that you have the jasmine
npm package installed globally. If that's the case, be aware that the globally installed package version can get out of sync with the one you have installed locally in your project.
LGTM, thanks very much for including these changes, and for the easy toggle-ability between yarn
and npm
commands. I would have happily bought the suggestion that these changes more properly belong on the create-react-app
repo, so I appreciate you making the effort here.
Thanks for the feedback.
Background:
I'm trying to follow along to the Jasmine install instructions for a brand-new React app. I run
npm create-react-app teardown3
. Then I begin following the aforementioned instructions:Using NPM (instead of
yarn
as mentioned in the instructions), I install the following dependencies:npm run jasmine init
failed (npm ERR! missing script: jasmine
), I ranjasmine init
by itself.spec/helpers/babel.js
and added the following code:package.json
, at the same level of nesting asdevDependencies
,browserList
, etc.:spec/helpers/jsdom.js
and added the following:const dom = new JSDOM('
'); global.document = dom.window.document; global.window = dom.window; global.navigator = dom.window.navigator;"helpers": [ "helpers/*/.?(m)js" ],
"helpers": [ "helpers/babel.js", "helpers/*/.js" ],
"test": "react-scripts test",
"test": "cross-env NODE_ENV=test jasmine",
import 'ignore-styles';
$ npm test
Randomized with seed 22274 Started F
Failures: 1) foo works Message: Expected true to equal false. Stack: Error: Expected true to equal false. at
at UserContext. (/Users/richiethomas/Desktop/Workspace/temp_react/teardown3/spec/foo.spec.js:5:18)
at
1 spec, 1 failure Finished in 0.01 seconds Randomized with seed 22274 (jasmine --random=true --seed=22274) npm ERR! Test failed. See above for more details.
import { render, screen } from '@testing-library/react';
describe('foo', () => { it('works', () => { expect(true).toEqual(false); }) })
{ "spec_dir": "spec", "spec_files": [ "*/[sS]pec.?(m)js" ], "helpers": [ "helpers/*/.?(m)js" ], "stopSpecOnExpectationFailure": false, "random": true }
{ "spec_dir": "src", "spec_files": [ "/.test." ], "helpers": [ "../spec/helpers/babel.js", "../spec/helpers//*.js" ], "stopSpecOnExpectationFailure": false, "random": true }
import { render, screen } from '@testing-library/react'; import App from './App';
test('renders learn react link', () => { render( );
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
$ npm test
ReferenceError: test is not defined at Object. (/Users/richiethomas/Desktop/Workspace/temp_react/teardown3/src/App.test.js:4:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module._compile (/Users/richiethomas/Desktop/Workspace/temp_react/teardown3/node_modules/pirates/lib/index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.newLoader [as .js] (/Users/richiethomas/Desktop/Workspace/temp_react/teardown3/node_modules/pirates/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module.load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Loader.requireShim [as require] (/Users/richiethomas/Desktop/Workspace/temp_react/teardown3/node_modules/jasmine/lib/loader.js:35:3)
npm ERR! Test failed. See above for more details.
$ npm test
Randomized with seed 62199 Started
Suite error: renders learn react link Message: ReferenceError: React is not defined Stack: ReferenceError: React is not defined at Suite. (/Users/richiethomas/Desktop/Workspace/temp_react/teardown3/src/App.test.js:5:10)
at
at Object. (/Users/richiethomas/Desktop/Workspace/temp_react/teardown3/src/App.test.js:4:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module._compile (/Users/richiethomas/Desktop/Workspace/temp_react/teardown3/node_modules/pirates/lib/index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.newLoader [as .js] (/Users/richiethomas/Desktop/Workspace/temp_react/teardown3/node_modules/pirates/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:928:32)
No specs found
Finished in 0.004 seconds
Randomized with seed 62199 (jasmine --random=true --seed=62199)
npm ERR! Test failed. See above for more details.
Suite error: renders learn react link Message: ReferenceError: React is not defined Stack: ReferenceError: React is not defined at App (/Users/richiethomas/Desktop/Workspace/temp_react/teardown3/src/App.js:5:3) ...
Suite error: renders learn react link Message: Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out Stack: Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out at
at Suite. (/Users/richiethomas/Desktop/Workspace/temp_react/teardown3/src/App.test.js:9:3)
import React from 'react'; import { render, screen } from '@testing-library/react'; import App from './App';
describe('renders learn react link', () => { it('runs the test', () => { render( );
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
})
});
Failures: 1) renders learn react link runs the test Message: TypeError: expect(...).toBeInTheDocument is not a function Stack: TypeError: expect(...).toBeInTheDocument is not a function at UserContext. (/Users/richiethomas/Desktop/Workspace/temp_react/teardown3/src/App.test.js:10:25)
at
at processImmediate (internal/timers.js:461:21)