how to use #providesModule with Jest and react-native - javascript

I'm trying to use Jest for unit testing react-native and I'm using the #providesModule successfully in the app, but Jest is failing to load the modules using this format
"import Component from "Component"
jest: version 13.2.3
react-native: 0.29.0
The error is:
FAIL js/components/home/__tests__/TestHome.js (0s)
Runtime Error
- Error: Cannot find module 'Time' from 'TimelineItem.js'
at Resolver.resolveModule (node_modules/jest-cli/node_modules/jest-resolve/build/index.js:197:17)
There is a small reference to 'providesModule and jest' on a blog post here
https://facebook.github.io/jest/blog/2016/06/22/jest-13.html
but it's not clear exactly what needs to be done.
If someone has #providesModule working with Jest and react-native, could you please help by indicating exactly what needs to be done?

with Jest 14.0 you can use Snapshot Test
You have to use React's react-test-renderer to render your component first

Related

How can I resolve a TypeError using old version of ember and ember-light-table?

I have the test project at https://github.com/ericg-ember-questions/test_computed_sort
I setup the project by doing the following:
Node version: v12.18.1 (npm v6.14.5)
npm install --save-dev ember-cli#3.4
./node_modules/.bin/ember new test_computed_sort
cd test_computed_sort/
./node_modules/.bin/ember install ember-light-table#1.13.2
./node_modules/.bin/ember generate component test-comp
./node_modules/.bin/ember serve
application.hbs
{{test-comp}}
test-comp.hbs
Hello
{{#light-table}}
{{/light-table}}
If I comment out the reference to light-table, no error is generated. However, with it, I see in the console:
media.js:15 Uncaught TypeError: decorator is not a function
at media.js:15:1
at Array.reduce (<anonymous>)
at _applyDecoratedDescriptor (media.js:15:1)
at Module.callback (media.js:240:1)
at Module.exports (loader.js:106:1)
at Module._reify (loader.js:143:1)
at Module.reify (loader.js:130:1)
at Module.exports (loader.js:104:1)
at requireModule (loader.js:27:1)
at Class._extractDefaultExport (index.js:432:1)
What can I do to resolve this error so I can use ember-light-table with this project?
The error kinda hints at this, but it doesn't really make sense unless you're using modern ember -- but you're using 3.4 (thanks for providing that information!!)
The stack in your error is actually very helpful, and here's how you can figure out what the issue is.
I downloaded your reproduction repo (thanks for providing that! reproductions are immensely helpful in debugging!)
I got the same error you did:
The key piece here is the media.js reference.
Clicking into that we see:
that the compiled version of the ember-responsive/services/media file is using decorators --
you have some version of ember-responsive in your app which has decorators.
I saw in your package.json that you're specifying on alder version:
"ember-responsive": "^4.0.2",
this v4 version of ember-responsive only supports ember 3.13 and higher.
Kinda poking around the ember-responsive github, https://github.com/freshbooks/ember-responsive/blob/v3.0.5/config/ember-try.js
I see that the v3 series of ember-responsive supports back to Ember 2.12 -- definitely before decorators before supported.
So downgrading ember-responsive and restarting the app reveals this error:
"Assertion Failed: [ember-light-table] table must be an instance of Table"
this error is documented here: https://github.com/adopted-ember-addons/ember-light-table/issues/726
so it looks like some APIs usage issue.
If you want help figuring that out, feel free to post another question.

Jest, Babel and Storybook, export vs module.exports

I am using Jest to test my services and Storybook and Babel to develop my React components.
I have a service which my component needs to use. I currently export with module.exports = someFunction; and it passes my Jest unit tests, but storybook tells me Cannot assign to read only property 'exports' of object '#<Object>' in the browser console.
If I change the service to export default someFunction, the component works but the Jest unit test fails with SyntaxError: Unexpected token export.
What do I do to make both work?
Notes:
I don't care whether I consistently export with module.exports or export. I have no real preference at this time.
I'm prepared to use a different unit-testing library, but I wasn't happy with Ava last time, and I don't remember having any luck with unit testing software and babel.
Last time I was using Babel I didn't have a problem with module.exports. Would a downgrade help? If so, what would I downgrade to?
Solved by adding a file called babel.config.js with the contents:
module.exports = {
presets: ['#babel/env']
};
If anyone has a better answer or believes they can educate me in the numerous areas I'm sure I'm shortcoming, they are welcome to do so.

Get `export {default as MyName} from './some-module';` to work with Jest (create-react-app)

I keep getting the error TypeError: Cannot read property 'default' of undefined when using the following syntax for re-exporting a module in JS:
export {default as MyName} from './some-module';
This works with create-react-app but the included Jest tests can't deal with it. Does Jest not use the same babel presets? Is there a way to get this to work?
It turns out I tricked myself into believing this to be syntax issue while it was actually a cyclical import problem.
It didn't turn up before because the application imported things in a different order than the tests.

Reflect.getOwnMetadata is not a function in karma with latest Aurelia

After updating to the latest version of Aurelia (March update beta.1.1.4), I'm getting the following error every time I run karma tests:
Error: Reflect.getOwnMetadata is not a function
Error loading C:/Software/myproject/test/unit/myclass.spec.ts
How do I fix it?
This has to do with the change of Aurelia from core-js to home-grown polyfills. The Reflect polyfill is missing and tests fail to run.
This problem is addressed in Aurelia navigation skeleton app by the following import statement in each unit test file:
import 'aurelia-polyfills';
I solved it by creating a setup.ts (or setup.js resp. to your language) file with just this statement, and then just listing it in karma.config.js at the first place.
for JS e.g.:
jspm : {
loadFiles: ['test/unit/setup.js', 'test/unit/**/*.js'],
...
}
for Typescript e.g.:
files: ['test/unit/setup.ts', 'test/unit/**/*.ts'],
...

Using sinon mocks with nodeunit

I'm learning to use sinon with nodeunit, specifically to do mocking. The recommended approach is to use sinon-nodeunit. According to the documentation, mocks should be available via the mock method of the test parameter to each test function.
However, this doesn't seem to work. If I run nodeunit on a file containing the following —
require('sinon-nodeunit');
exports['test sinon-nodeunit'] = function (test) {
mock = test.mock({});
test.done();
};
— I get this:
$ nodeunit test/test-sinon-nodeunit.js
test-sinon-nodeunit.js
✖ test sinon-nodeunit
TypeError: Object #<Object> has no method 'mock'
at /home/malkovich/test/test-sinon-nodeunit.js:4:17
at Object.runTest (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:54:9)
at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:90:21
at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:508:13
at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:118:13
at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:134:9
at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:507:9
at Object.concatSeries (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:147:23)
at Object.runSuite (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:79:11)
at Object.runModule (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:114:13)
FAILURES: 1/1 assertions failed (84ms)
I've installed current versions of both sinon (1.1.1) and sinon-nodeunit (0.1.2) via npm install.
require.path was removed in node 0.5.x and higher, which sinon-nodeunit uses. Christian Johansen just pushed a change in sinon-nodeunit which should be compatible with node 0.6.2 (and 0.5.x-series) which should fix this... It's already available on sinon-nodeunits github page, still hasnt hit npm though...
update: should be mentioned that sinon-nodeunit has been discontinued, as the creator is full focused on BusterJS which IMHO is a much better framework.

Categories

Resources