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

Throw when moduleNameMapper points to inexistent module #3567

Merged
merged 27 commits into from
Jun 29, 2017

Conversation

lindgr3n
Copy link
Contributor

@lindgr3n lindgr3n commented May 14, 2017

Summary
Fixes #1888
Should get error and test fail if the module configured in moduleNameMapper is not found.

Right now if the module is not found the test still passes. Test should fail so user is aware that the configuration is wrong.

Test plan
Need to add two test.

  • One providing a module that exist
  • One providing a module that don't exist
    Tried to get something to work but could need some pointers on how to mock out some parts. So left it out for now.

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Can you also add a test to verify throwing error works?

@@ -16,6 +16,8 @@ import type {ModuleMap} from 'types/HasteMap';
const path = require('path');
const nodeModulesPaths = require('resolve/lib/node-modules-paths');
const isBuiltinModule = require('is-builtin-module');
const chalk = require('chalk');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to add chalk to jest-resolve's dependencies.

})
);
});
if(!module) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to run npm run prettier to fix some styling issues.

@lindgr3n
Copy link
Contributor Author

Commited prettier and chalk dependencies change.

@lindgr3n
Copy link
Contributor Author

@thymikee Working on the test but not sure on how to implement the test.
My first attempt is something like

it('Should throw error when provided module that dont exist', () => {
const moduleMap = new ModuleMap();
const resolver = new Resolver(moduleMap, {});
expect(resolver.getMockModule('./', 'moduleThatDontExist')).toThrow();
});

But this get undefined error in ModuleMap. Not sure on what i should pass down to getMockModule so it reaches this._resolveStubModuleName(from, name); Any hint on where i could look?

@thymikee
Copy link
Collaborator

You need to feed Resolver with some data in constructor (e.g. moduleNameMapper option). If you find this test hard to do, you can always write an integration test.

@lindgr3n
Copy link
Contributor Author

@thymikee Not sure if the integration test is in the correct place or correct written, but its a start.

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I've left some inline comments but it's heading in the right direction. Make sure to remove 'use strict' and @flow calls from test files.

* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+jsinfra
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this

*
* @emails oncall+jsinfra
*/
'use strict';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use strict is no longer necessary, babel transform adds this for us

test('moduleNameMapper wrong configuration', () => {
const result = runJest('moduleNameMapper-wrong-config');
// Should fail because that wrong configuration will throw
expect(result.status).toBe(1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also catch a snapshot if this error? It's great that we know it fails, but even better if we see what's thrown


test('moduleNameMapping correct configuration', () => {
importedFn();
expect(importedFn.mock.calls.length).toBe(1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather like not to mock here, but meh, I guess it's fine

"\\.(css|less)$": "./__mocks__/styleMock.js"
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add newline

@@ -0,0 +1,23 @@
/**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use camelCase name for this file?

* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flow is not necessary here

@lindgr3n
Copy link
Contributor Author

@thymikee Thx for the feedback! Think i managed to fix the things you pointed out.
Refactored to use snapshot test.
Removed mock in testfiles.
Unsure if you wanted the full copyright header to be removed in moduleNameMapper.test.js?

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more round and after merging #3587 and we're good to go. Please rebase your branch to master.

Configuration error:

Unknown module in configuration option moduleNameMapper
Please check: /\\\\.(css|less)$/: module-that-dont-exist
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This double error is fixed here: #3587

@@ -0,0 +1,18 @@
const runJest = require('../runJest');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add copyright headers to every JS file. I mean just like this:

/**
 * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

Without 'use strict'; (rebase to master to see why) without @flow (we're currently not type-checking our tests) and without @emails...

*/

'use strict';
// Inlcude style so moduleNameMapping triggers
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is unnecessary

@@ -0,0 +1,7 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation, change to 2 per tab instead of 4.

*/

'use strict';
// Inlcude style so moduleNameMapping triggers
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment

@@ -0,0 +1,7 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation

{
"jest": {
"moduleNameMapper": {
"\\.(css|less)$": "module-that-dont-exist"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"module-that-dont-exist" -> "no-such-module" maybe? Your name has grammar issues.

@lindgr3n
Copy link
Contributor Author

@thymikee First time doing a rebase.
Is it enough to do "git rebase master" or do i need some more actions?

@thymikee
Copy link
Collaborator

When on your branch you should be good with:

git pull -r upstream master

Assuming upstream is the name of original Jest remote.
You can look it up with git remote -v.

@lindgr3n
Copy link
Contributor Author

Looks like i messed that up somehow.
$ git remote -v
origin git@github.com:lindgr3n/jest.git (fetch)
origin git@github.com:lindgr3n/jest.git (push)
So guess i need to
$ git remote add upstream https://github.com/facebook/jest.git
Then i can go on with
$ git pull -r upstream master

@thymikee
Copy link
Collaborator

Exactly

@lindgr3n
Copy link
Contributor Author

Just checking so im not heading in the wrong direction.

Your branch and 'origin/issue-1888' have diverged,
and have 13 and 6 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean

$ git pull
Merge made by the 'recursive' strategy.
$ git status

On branch issue-1888
Your branch is ahead of 'origin/issue-1888' by 14 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean

Just do a git push?
The 14 commits are they the sum from master and from my branch?

@thymikee
Copy link
Collaborator

Meh, just push it then.

@lindgr3n
Copy link
Contributor Author

Ty, back on track!

@lindgr3n
Copy link
Contributor Author

Looks like #3587 didn't merge. Still got double Configuration in the snapshot.

@lindgr3n
Copy link
Contributor Author

Ahh it haven't been merged yet :)

@thymikee
Copy link
Collaborator

thymikee commented May 17, 2017

Hey @lindgr3n, I've fixed some little nits and rebased your work on top of master properly, hope you don't mind. We still need to figure out why some errors are having duplicated messages, but it's definitely not in scope of this PR.

You'll be better off to hard reset to this branch to avoid local conflicts:

git reset --hard origin issue-1888

@lindgr3n
Copy link
Contributor Author

Nice, no problem. Good work!
Need to learn how to rebase correct next time :)

@lindgr3n
Copy link
Contributor Author

$ git reset --hard origin issue-1888
fatal: Cannot do hard reset with paths.
Did a
$ git reset --har issue-1888
But then i got into conflict state.
Guess it would be easier to just do a new checkout of the branch?

@thymikee
Copy link
Collaborator

git reset --hard origin/issue-1888 should fix that

@lindgr3n
Copy link
Contributor Author

Ahh ty! Should have guessed that it should have been a / in the command.
Now it is correct!

@lindgr3n
Copy link
Contributor Author

@thymikee Did i fix the changes you wanted or should i fix something more?

@michahell
Copy link

Could someone from JEST please look at this? A silently failing jest config is still something many newcomers to JEST are running into (many JEST ignore (s)css related SO posts etc.)

@thymikee
Copy link
Collaborator

There's an issue with this message being added twice, which we yet have to figure out: https://github.com/facebook/jest/pull/3567/files#diff-0454b3bf8e9e55c3655f3be2ff46cf28R19
I just didn't have time to look at this.

@thymikee
Copy link
Collaborator

@lindgr3n sorry about that, but I've fixed the branch for you.

However, you could definitely use some training in git rebasing.
This video series by @kentcdodds looks like a great way to start: https://egghead.io/lessons/javascript-how-to-rebase-a-git-pull-request-branch

I also fixed the message, it looks like this now:

screen shot 2017-06-27 at 22 29 45

Thanks for your work on that! Appreciated 👍

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's wait for CI to pass.

@cpojer
Copy link
Member

cpojer commented Jun 28, 2017

CI is unhappy, needs prettier.

@cpojer
Copy link
Member

cpojer commented Jun 28, 2017

Failing on windows.

@lindgr3n
Copy link
Contributor Author

Thanks again @thymikee . Appreciate the help!

Checked the commits and i'm unsure why "integration_tests/tests/auto_clear_mocks.test.js" is changed. Feels like it should not have been changed?

Also the snapshots for "module_name_mapper.test.js" need to be updated.

@thymikee
Copy link
Collaborator

Skipping the test on Windows. Feature works, it's just the test output differs between UNIX and Windows. I'm not going to fix this now, as other tests may be skipped for the same reason.

@thymikee thymikee changed the title Fixes issue-1888 Throw when moduleNameMapper points to inexistent module Jun 29, 2017
@cpojer cpojer merged commit 30f35c3 into jestjs:master Jun 29, 2017
@lindgr3n
Copy link
Contributor Author

Big thanks @thymikee for helping me with my first contribution to OSS! :)
Will work on the rebase until the next time ;)

Keep up the good work!

tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* Fixes issue-1888

* Prettier fix and chalk to package.json dependencies

* Added integration test

* Added integration test

* Refactored test to use snapshot

* Refactored test to use snapshot

* Rename to camelCase

* Fixed lint warnings

* Fixed lint warnings

* Minor fixes

* Use strict on integration tests

* Added integration test

* Refactored test to use snapshot

* Fixed lint warnings

* Added integration test

* Refactored test to use snapshot

* Minor fixes

* Use strict on integration tests

* Use Error instead of ValidationError to avoid duplicate message

* Add fb copyright back to auto_reset_mocks test

* Removed jest-validate dep from jest-resolve

* Run prettier

* Revert auto_clear_mocks change

* Revert auto_reset_mocks change

* Normalize test paths on windows

* Don't use slash to normalize summary

* Skip test on windows
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Errors in moduleNameMapper configuration seemingly ignored
6 participants