Javascript Test unit with Jest an ES6 module
too many divergent posts upon googling to choose a clear and up-to-date solution...
I wrote 3 tests to check different possibilities
===========. TEST 1 OK ================
// helloJest.js
function sayHello() {
return "hello there jest"
}
module.exports = sayHello;
// helloJestTest
const sayHello = require('../../src/client/js/helloJest');
test('string returning hello there jest', () => {//
expect(sayHello()).toEqual('hello there jest');
});
===========. TEST 2 FAILING ================
// helloJest.js
function sayHello() {
return "hello there jest"
}
export default { sayHello }; // <= changed
// helloJestTest
const sayHello = require('../../src/client/js/helloJest');
test('string returning hello there jest', () => {//
expect(sayHello()).toEqual('hello there jest');
});
TypeError: sayHello is not a function
3 |
4 | test('string returning hello there jest', () => {//
> 5 | expect(sayHello()).toEqual('hello there jest');
| ^
6 | });
7 |
===========. TEST 3 FAILING ================
// helloJest.js
function sayHello() {
return "hello there jest"
}
export default { sayHello }; // <= changed
// helloJestTest
import { sayHello } from '../../src/client/js/helloJest'; // <= changed
test('string returning hello there jest', () => {//
expect(sayHello()).toEqual('hello there jest');
});
TypeError: (0 , _helloJest.sayHello) is not a function
3 |
4 | test('string returning hello there jest', () => {//
> 5 | expect(sayHello()).toEqual('hello there jest');
| ^
6 | });
How to pass the TEST 3 correctly ???
I am using the following packages
package.json
"babel-core": "^6.26.3",
"babel-jest": "^23.6.0",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
...
"jest": {
"moduleFileExtensions": ["js"],
"transform": { "^.+\.js?$": "babel-jest" },
"testRegex": "/tests/.*\.(js)$"
}
and I have in
.babelrc
{
"presets": ["env"]
}
javascript ecmascript-6 jestjs babel testunit
add a comment |
too many divergent posts upon googling to choose a clear and up-to-date solution...
I wrote 3 tests to check different possibilities
===========. TEST 1 OK ================
// helloJest.js
function sayHello() {
return "hello there jest"
}
module.exports = sayHello;
// helloJestTest
const sayHello = require('../../src/client/js/helloJest');
test('string returning hello there jest', () => {//
expect(sayHello()).toEqual('hello there jest');
});
===========. TEST 2 FAILING ================
// helloJest.js
function sayHello() {
return "hello there jest"
}
export default { sayHello }; // <= changed
// helloJestTest
const sayHello = require('../../src/client/js/helloJest');
test('string returning hello there jest', () => {//
expect(sayHello()).toEqual('hello there jest');
});
TypeError: sayHello is not a function
3 |
4 | test('string returning hello there jest', () => {//
> 5 | expect(sayHello()).toEqual('hello there jest');
| ^
6 | });
7 |
===========. TEST 3 FAILING ================
// helloJest.js
function sayHello() {
return "hello there jest"
}
export default { sayHello }; // <= changed
// helloJestTest
import { sayHello } from '../../src/client/js/helloJest'; // <= changed
test('string returning hello there jest', () => {//
expect(sayHello()).toEqual('hello there jest');
});
TypeError: (0 , _helloJest.sayHello) is not a function
3 |
4 | test('string returning hello there jest', () => {//
> 5 | expect(sayHello()).toEqual('hello there jest');
| ^
6 | });
How to pass the TEST 3 correctly ???
I am using the following packages
package.json
"babel-core": "^6.26.3",
"babel-jest": "^23.6.0",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
...
"jest": {
"moduleFileExtensions": ["js"],
"transform": { "^.+\.js?$": "babel-jest" },
"testRegex": "/tests/.*\.(js)$"
}
and I have in
.babelrc
{
"presets": ["env"]
}
javascript ecmascript-6 jestjs babel testunit
What's the difference between 2 and 3? You only seem to have made half the change; in both cases the default export is an object, not just the function.
– jonrsharpe
Jan 1 at 18:01
did you try in your test2 to doconst { sayHello } = require('../../src/client/js/helloJest');
– quirimmo
Jan 1 at 18:03
In 2. ( export default ES6 , require() ) In 3. ( export default ES6, import ES6 )
– user762579
Jan 1 at 18:03
add a comment |
too many divergent posts upon googling to choose a clear and up-to-date solution...
I wrote 3 tests to check different possibilities
===========. TEST 1 OK ================
// helloJest.js
function sayHello() {
return "hello there jest"
}
module.exports = sayHello;
// helloJestTest
const sayHello = require('../../src/client/js/helloJest');
test('string returning hello there jest', () => {//
expect(sayHello()).toEqual('hello there jest');
});
===========. TEST 2 FAILING ================
// helloJest.js
function sayHello() {
return "hello there jest"
}
export default { sayHello }; // <= changed
// helloJestTest
const sayHello = require('../../src/client/js/helloJest');
test('string returning hello there jest', () => {//
expect(sayHello()).toEqual('hello there jest');
});
TypeError: sayHello is not a function
3 |
4 | test('string returning hello there jest', () => {//
> 5 | expect(sayHello()).toEqual('hello there jest');
| ^
6 | });
7 |
===========. TEST 3 FAILING ================
// helloJest.js
function sayHello() {
return "hello there jest"
}
export default { sayHello }; // <= changed
// helloJestTest
import { sayHello } from '../../src/client/js/helloJest'; // <= changed
test('string returning hello there jest', () => {//
expect(sayHello()).toEqual('hello there jest');
});
TypeError: (0 , _helloJest.sayHello) is not a function
3 |
4 | test('string returning hello there jest', () => {//
> 5 | expect(sayHello()).toEqual('hello there jest');
| ^
6 | });
How to pass the TEST 3 correctly ???
I am using the following packages
package.json
"babel-core": "^6.26.3",
"babel-jest": "^23.6.0",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
...
"jest": {
"moduleFileExtensions": ["js"],
"transform": { "^.+\.js?$": "babel-jest" },
"testRegex": "/tests/.*\.(js)$"
}
and I have in
.babelrc
{
"presets": ["env"]
}
javascript ecmascript-6 jestjs babel testunit
too many divergent posts upon googling to choose a clear and up-to-date solution...
I wrote 3 tests to check different possibilities
===========. TEST 1 OK ================
// helloJest.js
function sayHello() {
return "hello there jest"
}
module.exports = sayHello;
// helloJestTest
const sayHello = require('../../src/client/js/helloJest');
test('string returning hello there jest', () => {//
expect(sayHello()).toEqual('hello there jest');
});
===========. TEST 2 FAILING ================
// helloJest.js
function sayHello() {
return "hello there jest"
}
export default { sayHello }; // <= changed
// helloJestTest
const sayHello = require('../../src/client/js/helloJest');
test('string returning hello there jest', () => {//
expect(sayHello()).toEqual('hello there jest');
});
TypeError: sayHello is not a function
3 |
4 | test('string returning hello there jest', () => {//
> 5 | expect(sayHello()).toEqual('hello there jest');
| ^
6 | });
7 |
===========. TEST 3 FAILING ================
// helloJest.js
function sayHello() {
return "hello there jest"
}
export default { sayHello }; // <= changed
// helloJestTest
import { sayHello } from '../../src/client/js/helloJest'; // <= changed
test('string returning hello there jest', () => {//
expect(sayHello()).toEqual('hello there jest');
});
TypeError: (0 , _helloJest.sayHello) is not a function
3 |
4 | test('string returning hello there jest', () => {//
> 5 | expect(sayHello()).toEqual('hello there jest');
| ^
6 | });
How to pass the TEST 3 correctly ???
I am using the following packages
package.json
"babel-core": "^6.26.3",
"babel-jest": "^23.6.0",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
...
"jest": {
"moduleFileExtensions": ["js"],
"transform": { "^.+\.js?$": "babel-jest" },
"testRegex": "/tests/.*\.(js)$"
}
and I have in
.babelrc
{
"presets": ["env"]
}
javascript ecmascript-6 jestjs babel testunit
javascript ecmascript-6 jestjs babel testunit
edited Jan 18 at 21:24
Popo
2,06242348
2,06242348
asked Jan 1 at 17:58
user762579
What's the difference between 2 and 3? You only seem to have made half the change; in both cases the default export is an object, not just the function.
– jonrsharpe
Jan 1 at 18:01
did you try in your test2 to doconst { sayHello } = require('../../src/client/js/helloJest');
– quirimmo
Jan 1 at 18:03
In 2. ( export default ES6 , require() ) In 3. ( export default ES6, import ES6 )
– user762579
Jan 1 at 18:03
add a comment |
What's the difference between 2 and 3? You only seem to have made half the change; in both cases the default export is an object, not just the function.
– jonrsharpe
Jan 1 at 18:01
did you try in your test2 to doconst { sayHello } = require('../../src/client/js/helloJest');
– quirimmo
Jan 1 at 18:03
In 2. ( export default ES6 , require() ) In 3. ( export default ES6, import ES6 )
– user762579
Jan 1 at 18:03
What's the difference between 2 and 3? You only seem to have made half the change; in both cases the default export is an object, not just the function.
– jonrsharpe
Jan 1 at 18:01
What's the difference between 2 and 3? You only seem to have made half the change; in both cases the default export is an object, not just the function.
– jonrsharpe
Jan 1 at 18:01
did you try in your test2 to do
const { sayHello } = require('../../src/client/js/helloJest');
– quirimmo
Jan 1 at 18:03
did you try in your test2 to do
const { sayHello } = require('../../src/client/js/helloJest');
– quirimmo
Jan 1 at 18:03
In 2. ( export default ES6 , require() ) In 3. ( export default ES6, import ES6 )
– user762579
Jan 1 at 18:03
In 2. ( export default ES6 , require() ) In 3. ( export default ES6, import ES6 )
– user762579
Jan 1 at 18:03
add a comment |
2 Answers
2
active
oldest
votes
You're tripping up in a couple of places there. Primarily: You don't use {}
with the default import/export.
This:
export default { sayHello };
exports an object as the default export of the module. The object has a single property, sayHello
, referring to the function. To make the function the default export, don't use the {}
:
export default sayHello;
Then, when importing, if you want the default import, don't use {}
:
import sayHello from '../../src/client/js/helloJest';
If you want to export a named export, you do use {}
:
export { sayHello };
and
import { sayHello } from '../../src/client/js/helloJest';
Examples of both on plunker: https://embed.plnkr.co/f8PhOhULphQUrVXwdXF3/
1
@@T.J. Good .. clear answer ... I'm still mixing it ...
– user762579
Jan 1 at 18:06
it's ok with default... however it seems that Jest does not like import a named export ....
– user762579
Jan 1 at 18:11
@erwin - You're sure you're exporting it correctly? It would be odd if Jest had a problem with named exports.
– T.J. Crowder
Jan 1 at 18:16
1
@erwin one thing that tripped me up for a while.{}
is not destructuring. It is just importing named exports from the module.
– evolutionxbox
Jan 2 at 22:38
add a comment |
TEST 2
You do export as default an object with a single property which is the sayHello
function, so you should import it in jest through the following:
const { sayHello } = require('../../src/client/js/helloJest');
TEST 3
Again you do export as above.
In this case you can import it as the following:
import Hello from '../../src/client/js/helloJest';
And then you should be able to use your func as:
Hello.sayHello
1
That's it !!! I got it right now in Test 3 ... and understood all 3 tests ... thanks a lot !!!
– user762579
Jan 1 at 18:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997690%2fjavascript-test-unit-with-jest-an-es6-module%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're tripping up in a couple of places there. Primarily: You don't use {}
with the default import/export.
This:
export default { sayHello };
exports an object as the default export of the module. The object has a single property, sayHello
, referring to the function. To make the function the default export, don't use the {}
:
export default sayHello;
Then, when importing, if you want the default import, don't use {}
:
import sayHello from '../../src/client/js/helloJest';
If you want to export a named export, you do use {}
:
export { sayHello };
and
import { sayHello } from '../../src/client/js/helloJest';
Examples of both on plunker: https://embed.plnkr.co/f8PhOhULphQUrVXwdXF3/
1
@@T.J. Good .. clear answer ... I'm still mixing it ...
– user762579
Jan 1 at 18:06
it's ok with default... however it seems that Jest does not like import a named export ....
– user762579
Jan 1 at 18:11
@erwin - You're sure you're exporting it correctly? It would be odd if Jest had a problem with named exports.
– T.J. Crowder
Jan 1 at 18:16
1
@erwin one thing that tripped me up for a while.{}
is not destructuring. It is just importing named exports from the module.
– evolutionxbox
Jan 2 at 22:38
add a comment |
You're tripping up in a couple of places there. Primarily: You don't use {}
with the default import/export.
This:
export default { sayHello };
exports an object as the default export of the module. The object has a single property, sayHello
, referring to the function. To make the function the default export, don't use the {}
:
export default sayHello;
Then, when importing, if you want the default import, don't use {}
:
import sayHello from '../../src/client/js/helloJest';
If you want to export a named export, you do use {}
:
export { sayHello };
and
import { sayHello } from '../../src/client/js/helloJest';
Examples of both on plunker: https://embed.plnkr.co/f8PhOhULphQUrVXwdXF3/
1
@@T.J. Good .. clear answer ... I'm still mixing it ...
– user762579
Jan 1 at 18:06
it's ok with default... however it seems that Jest does not like import a named export ....
– user762579
Jan 1 at 18:11
@erwin - You're sure you're exporting it correctly? It would be odd if Jest had a problem with named exports.
– T.J. Crowder
Jan 1 at 18:16
1
@erwin one thing that tripped me up for a while.{}
is not destructuring. It is just importing named exports from the module.
– evolutionxbox
Jan 2 at 22:38
add a comment |
You're tripping up in a couple of places there. Primarily: You don't use {}
with the default import/export.
This:
export default { sayHello };
exports an object as the default export of the module. The object has a single property, sayHello
, referring to the function. To make the function the default export, don't use the {}
:
export default sayHello;
Then, when importing, if you want the default import, don't use {}
:
import sayHello from '../../src/client/js/helloJest';
If you want to export a named export, you do use {}
:
export { sayHello };
and
import { sayHello } from '../../src/client/js/helloJest';
Examples of both on plunker: https://embed.plnkr.co/f8PhOhULphQUrVXwdXF3/
You're tripping up in a couple of places there. Primarily: You don't use {}
with the default import/export.
This:
export default { sayHello };
exports an object as the default export of the module. The object has a single property, sayHello
, referring to the function. To make the function the default export, don't use the {}
:
export default sayHello;
Then, when importing, if you want the default import, don't use {}
:
import sayHello from '../../src/client/js/helloJest';
If you want to export a named export, you do use {}
:
export { sayHello };
and
import { sayHello } from '../../src/client/js/helloJest';
Examples of both on plunker: https://embed.plnkr.co/f8PhOhULphQUrVXwdXF3/
edited Jan 1 at 18:08
answered Jan 1 at 18:03
T.J. CrowderT.J. Crowder
693k12212331328
693k12212331328
1
@@T.J. Good .. clear answer ... I'm still mixing it ...
– user762579
Jan 1 at 18:06
it's ok with default... however it seems that Jest does not like import a named export ....
– user762579
Jan 1 at 18:11
@erwin - You're sure you're exporting it correctly? It would be odd if Jest had a problem with named exports.
– T.J. Crowder
Jan 1 at 18:16
1
@erwin one thing that tripped me up for a while.{}
is not destructuring. It is just importing named exports from the module.
– evolutionxbox
Jan 2 at 22:38
add a comment |
1
@@T.J. Good .. clear answer ... I'm still mixing it ...
– user762579
Jan 1 at 18:06
it's ok with default... however it seems that Jest does not like import a named export ....
– user762579
Jan 1 at 18:11
@erwin - You're sure you're exporting it correctly? It would be odd if Jest had a problem with named exports.
– T.J. Crowder
Jan 1 at 18:16
1
@erwin one thing that tripped me up for a while.{}
is not destructuring. It is just importing named exports from the module.
– evolutionxbox
Jan 2 at 22:38
1
1
@@T.J. Good .. clear answer ... I'm still mixing it ...
– user762579
Jan 1 at 18:06
@@T.J. Good .. clear answer ... I'm still mixing it ...
– user762579
Jan 1 at 18:06
it's ok with default... however it seems that Jest does not like import a named export ....
– user762579
Jan 1 at 18:11
it's ok with default... however it seems that Jest does not like import a named export ....
– user762579
Jan 1 at 18:11
@erwin - You're sure you're exporting it correctly? It would be odd if Jest had a problem with named exports.
– T.J. Crowder
Jan 1 at 18:16
@erwin - You're sure you're exporting it correctly? It would be odd if Jest had a problem with named exports.
– T.J. Crowder
Jan 1 at 18:16
1
1
@erwin one thing that tripped me up for a while.
{}
is not destructuring. It is just importing named exports from the module.– evolutionxbox
Jan 2 at 22:38
@erwin one thing that tripped me up for a while.
{}
is not destructuring. It is just importing named exports from the module.– evolutionxbox
Jan 2 at 22:38
add a comment |
TEST 2
You do export as default an object with a single property which is the sayHello
function, so you should import it in jest through the following:
const { sayHello } = require('../../src/client/js/helloJest');
TEST 3
Again you do export as above.
In this case you can import it as the following:
import Hello from '../../src/client/js/helloJest';
And then you should be able to use your func as:
Hello.sayHello
1
That's it !!! I got it right now in Test 3 ... and understood all 3 tests ... thanks a lot !!!
– user762579
Jan 1 at 18:18
add a comment |
TEST 2
You do export as default an object with a single property which is the sayHello
function, so you should import it in jest through the following:
const { sayHello } = require('../../src/client/js/helloJest');
TEST 3
Again you do export as above.
In this case you can import it as the following:
import Hello from '../../src/client/js/helloJest';
And then you should be able to use your func as:
Hello.sayHello
1
That's it !!! I got it right now in Test 3 ... and understood all 3 tests ... thanks a lot !!!
– user762579
Jan 1 at 18:18
add a comment |
TEST 2
You do export as default an object with a single property which is the sayHello
function, so you should import it in jest through the following:
const { sayHello } = require('../../src/client/js/helloJest');
TEST 3
Again you do export as above.
In this case you can import it as the following:
import Hello from '../../src/client/js/helloJest';
And then you should be able to use your func as:
Hello.sayHello
TEST 2
You do export as default an object with a single property which is the sayHello
function, so you should import it in jest through the following:
const { sayHello } = require('../../src/client/js/helloJest');
TEST 3
Again you do export as above.
In this case you can import it as the following:
import Hello from '../../src/client/js/helloJest';
And then you should be able to use your func as:
Hello.sayHello
answered Jan 1 at 18:08
quirimmoquirimmo
7,66811334
7,66811334
1
That's it !!! I got it right now in Test 3 ... and understood all 3 tests ... thanks a lot !!!
– user762579
Jan 1 at 18:18
add a comment |
1
That's it !!! I got it right now in Test 3 ... and understood all 3 tests ... thanks a lot !!!
– user762579
Jan 1 at 18:18
1
1
That's it !!! I got it right now in Test 3 ... and understood all 3 tests ... thanks a lot !!!
– user762579
Jan 1 at 18:18
That's it !!! I got it right now in Test 3 ... and understood all 3 tests ... thanks a lot !!!
– user762579
Jan 1 at 18:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997690%2fjavascript-test-unit-with-jest-an-es6-module%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's the difference between 2 and 3? You only seem to have made half the change; in both cases the default export is an object, not just the function.
– jonrsharpe
Jan 1 at 18:01
did you try in your test2 to do
const { sayHello } = require('../../src/client/js/helloJest');
– quirimmo
Jan 1 at 18:03
In 2. ( export default ES6 , require() ) In 3. ( export default ES6, import ES6 )
– user762579
Jan 1 at 18:03