Unit Testing ES6 Class with External Dependency
I'm trying to set a unit testing boilerplate for my company. Our front end projects are built with ES6 classes and have a dependency to our core product. The front end code gets wrapped through a build process in a whole other block of code that is basically a closure and captures the dependency. So we don't have to manually import it in order to use it.
Let's say the dependency is called productScope
and it's an object that has some DOM models, internal APIs and parameters among many other things necessary for each project. At the moment, Mocha throws ReferenceError: productScope is not defined.
How can I mock this object? Or should I just use the actual object?
Example:
class someClass {
constructor() {
const id = productScope.items[0].id
const item = productScope.domModel.querySelector('.some-div')
item.classList.add(`added-${id}`)
}
}
This get wrapped in core code like below:
(function(productScope) {
// front end code goes here
}(productScope)
Testing file:
import someClass from '../../js/someClass'
describe('someClass', function() {
const someClass = new someClass()
it('should be a class', function() {
console.log(someClass)
});
});
javascript unit-testing mocking mocha
add a comment |
I'm trying to set a unit testing boilerplate for my company. Our front end projects are built with ES6 classes and have a dependency to our core product. The front end code gets wrapped through a build process in a whole other block of code that is basically a closure and captures the dependency. So we don't have to manually import it in order to use it.
Let's say the dependency is called productScope
and it's an object that has some DOM models, internal APIs and parameters among many other things necessary for each project. At the moment, Mocha throws ReferenceError: productScope is not defined.
How can I mock this object? Or should I just use the actual object?
Example:
class someClass {
constructor() {
const id = productScope.items[0].id
const item = productScope.domModel.querySelector('.some-div')
item.classList.add(`added-${id}`)
}
}
This get wrapped in core code like below:
(function(productScope) {
// front end code goes here
}(productScope)
Testing file:
import someClass from '../../js/someClass'
describe('someClass', function() {
const someClass = new someClass()
it('should be a class', function() {
console.log(someClass)
});
});
javascript unit-testing mocking mocha
Please edit your question to include relevant parts of your unit test code
– Patrick Hund
Nov 20 at 17:18
Here is an answer that is in the style of your question: Read this: gofreerange.com/mocha/docs/Mocha/Mock.html
– Randy Casburn
Nov 20 at 17:25
@PatrickHund added some code
– Jaeeun Lee
Nov 20 at 17:39
add a comment |
I'm trying to set a unit testing boilerplate for my company. Our front end projects are built with ES6 classes and have a dependency to our core product. The front end code gets wrapped through a build process in a whole other block of code that is basically a closure and captures the dependency. So we don't have to manually import it in order to use it.
Let's say the dependency is called productScope
and it's an object that has some DOM models, internal APIs and parameters among many other things necessary for each project. At the moment, Mocha throws ReferenceError: productScope is not defined.
How can I mock this object? Or should I just use the actual object?
Example:
class someClass {
constructor() {
const id = productScope.items[0].id
const item = productScope.domModel.querySelector('.some-div')
item.classList.add(`added-${id}`)
}
}
This get wrapped in core code like below:
(function(productScope) {
// front end code goes here
}(productScope)
Testing file:
import someClass from '../../js/someClass'
describe('someClass', function() {
const someClass = new someClass()
it('should be a class', function() {
console.log(someClass)
});
});
javascript unit-testing mocking mocha
I'm trying to set a unit testing boilerplate for my company. Our front end projects are built with ES6 classes and have a dependency to our core product. The front end code gets wrapped through a build process in a whole other block of code that is basically a closure and captures the dependency. So we don't have to manually import it in order to use it.
Let's say the dependency is called productScope
and it's an object that has some DOM models, internal APIs and parameters among many other things necessary for each project. At the moment, Mocha throws ReferenceError: productScope is not defined.
How can I mock this object? Or should I just use the actual object?
Example:
class someClass {
constructor() {
const id = productScope.items[0].id
const item = productScope.domModel.querySelector('.some-div')
item.classList.add(`added-${id}`)
}
}
This get wrapped in core code like below:
(function(productScope) {
// front end code goes here
}(productScope)
Testing file:
import someClass from '../../js/someClass'
describe('someClass', function() {
const someClass = new someClass()
it('should be a class', function() {
console.log(someClass)
});
});
javascript unit-testing mocking mocha
javascript unit-testing mocking mocha
edited Nov 26 at 16:46
asked Nov 20 at 17:09
Jaeeun Lee
1,08092542
1,08092542
Please edit your question to include relevant parts of your unit test code
– Patrick Hund
Nov 20 at 17:18
Here is an answer that is in the style of your question: Read this: gofreerange.com/mocha/docs/Mocha/Mock.html
– Randy Casburn
Nov 20 at 17:25
@PatrickHund added some code
– Jaeeun Lee
Nov 20 at 17:39
add a comment |
Please edit your question to include relevant parts of your unit test code
– Patrick Hund
Nov 20 at 17:18
Here is an answer that is in the style of your question: Read this: gofreerange.com/mocha/docs/Mocha/Mock.html
– Randy Casburn
Nov 20 at 17:25
@PatrickHund added some code
– Jaeeun Lee
Nov 20 at 17:39
Please edit your question to include relevant parts of your unit test code
– Patrick Hund
Nov 20 at 17:18
Please edit your question to include relevant parts of your unit test code
– Patrick Hund
Nov 20 at 17:18
Here is an answer that is in the style of your question: Read this: gofreerange.com/mocha/docs/Mocha/Mock.html
– Randy Casburn
Nov 20 at 17:25
Here is an answer that is in the style of your question: Read this: gofreerange.com/mocha/docs/Mocha/Mock.html
– Randy Casburn
Nov 20 at 17:25
@PatrickHund added some code
– Jaeeun Lee
Nov 20 at 17:39
@PatrickHund added some code
– Jaeeun Lee
Nov 20 at 17:39
add a comment |
3 Answers
3
active
oldest
votes
Looks like productScope
is a global variable.
Something like this should work for you.
import someClass from '../../js/someClass';
describe('someClass', function() {
let someClass;
beforeEach(() => {
global.productScope = {
// you mock definition
someClass = new someClass();
};
});
it('should be a class', function() {
console.log(someClass)
});
});
Thanks, but still the same error, plus1) "before each" hook for "should be a class"
message in red.
– Jaeeun Lee
Nov 21 at 1:02
Where are you gettingproductScope
from? Are you importing it in your file or is it a global variable?
– Dinesh Pandiyan
Nov 21 at 1:04
Basically, there is a whole block of code that wraps the front end code, and it's a closure that capturesproductScope
– Jaeeun Lee
Nov 21 at 1:06
If it's a closure, you could stub it withsinon
. Can you show how you importproductScope
in thejs/someClass
file?
– Dinesh Pandiyan
Nov 21 at 1:52
I don’t need to import it to the class because the class is kind of wrapped in it. Perhaps like how you’d use the $ as jQuery inside of an IIFE.
– Jaeeun Lee
Nov 21 at 5:33
|
show 2 more comments
You can try something like this
describe('#someClass', () => {
let someClass;
beforeEach(() => {
global.productScope = {
// mocking productScope object
};
});
it('should be a class', () => {
someClass = new SomeClass;
console.log(someClass);
});
afterEach(() => {
delete global.productScope;
});
});
or alternatively if you want more specific mock logic for each test case
describe('#someClass', () => {
let someClass;
it('should be a class', () => {
global.productScope = {
// mocking productScope object
};
// Test logic start
someClass = new SomeClass;
console.log(someClass);
// Test logic end
delete global.productScope;
});
});
add a comment |
I'm with other answers as well, as managing global
variables seems to be the simplest and most straightforward solution.
However, you can use toString to get class's string representation, and eval it to bind to closure's scope:
class someClass {
constructor() {
this.id = scopedId
}
}
// pass class as an argument
function scopeFactory(classDef) {
// define scoped data
let scopedId = 2;
// eval is used to bind class to the local closure
// so `scopedId` will be in charge
return eval("(" + classDef + ")");
}
const scopedSomeClass = scopeFactory(someClass);
console.log(new scopedSomeClass)
Note that eval(someCLass.toString())
doesn't work without parentheses.
You can add it as a helper function, into your project.
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%2f53398084%2funit-testing-es6-class-with-external-dependency%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Looks like productScope
is a global variable.
Something like this should work for you.
import someClass from '../../js/someClass';
describe('someClass', function() {
let someClass;
beforeEach(() => {
global.productScope = {
// you mock definition
someClass = new someClass();
};
});
it('should be a class', function() {
console.log(someClass)
});
});
Thanks, but still the same error, plus1) "before each" hook for "should be a class"
message in red.
– Jaeeun Lee
Nov 21 at 1:02
Where are you gettingproductScope
from? Are you importing it in your file or is it a global variable?
– Dinesh Pandiyan
Nov 21 at 1:04
Basically, there is a whole block of code that wraps the front end code, and it's a closure that capturesproductScope
– Jaeeun Lee
Nov 21 at 1:06
If it's a closure, you could stub it withsinon
. Can you show how you importproductScope
in thejs/someClass
file?
– Dinesh Pandiyan
Nov 21 at 1:52
I don’t need to import it to the class because the class is kind of wrapped in it. Perhaps like how you’d use the $ as jQuery inside of an IIFE.
– Jaeeun Lee
Nov 21 at 5:33
|
show 2 more comments
Looks like productScope
is a global variable.
Something like this should work for you.
import someClass from '../../js/someClass';
describe('someClass', function() {
let someClass;
beforeEach(() => {
global.productScope = {
// you mock definition
someClass = new someClass();
};
});
it('should be a class', function() {
console.log(someClass)
});
});
Thanks, but still the same error, plus1) "before each" hook for "should be a class"
message in red.
– Jaeeun Lee
Nov 21 at 1:02
Where are you gettingproductScope
from? Are you importing it in your file or is it a global variable?
– Dinesh Pandiyan
Nov 21 at 1:04
Basically, there is a whole block of code that wraps the front end code, and it's a closure that capturesproductScope
– Jaeeun Lee
Nov 21 at 1:06
If it's a closure, you could stub it withsinon
. Can you show how you importproductScope
in thejs/someClass
file?
– Dinesh Pandiyan
Nov 21 at 1:52
I don’t need to import it to the class because the class is kind of wrapped in it. Perhaps like how you’d use the $ as jQuery inside of an IIFE.
– Jaeeun Lee
Nov 21 at 5:33
|
show 2 more comments
Looks like productScope
is a global variable.
Something like this should work for you.
import someClass from '../../js/someClass';
describe('someClass', function() {
let someClass;
beforeEach(() => {
global.productScope = {
// you mock definition
someClass = new someClass();
};
});
it('should be a class', function() {
console.log(someClass)
});
});
Looks like productScope
is a global variable.
Something like this should work for you.
import someClass from '../../js/someClass';
describe('someClass', function() {
let someClass;
beforeEach(() => {
global.productScope = {
// you mock definition
someClass = new someClass();
};
});
it('should be a class', function() {
console.log(someClass)
});
});
answered Nov 21 at 0:09
Dinesh Pandiyan
2,293825
2,293825
Thanks, but still the same error, plus1) "before each" hook for "should be a class"
message in red.
– Jaeeun Lee
Nov 21 at 1:02
Where are you gettingproductScope
from? Are you importing it in your file or is it a global variable?
– Dinesh Pandiyan
Nov 21 at 1:04
Basically, there is a whole block of code that wraps the front end code, and it's a closure that capturesproductScope
– Jaeeun Lee
Nov 21 at 1:06
If it's a closure, you could stub it withsinon
. Can you show how you importproductScope
in thejs/someClass
file?
– Dinesh Pandiyan
Nov 21 at 1:52
I don’t need to import it to the class because the class is kind of wrapped in it. Perhaps like how you’d use the $ as jQuery inside of an IIFE.
– Jaeeun Lee
Nov 21 at 5:33
|
show 2 more comments
Thanks, but still the same error, plus1) "before each" hook for "should be a class"
message in red.
– Jaeeun Lee
Nov 21 at 1:02
Where are you gettingproductScope
from? Are you importing it in your file or is it a global variable?
– Dinesh Pandiyan
Nov 21 at 1:04
Basically, there is a whole block of code that wraps the front end code, and it's a closure that capturesproductScope
– Jaeeun Lee
Nov 21 at 1:06
If it's a closure, you could stub it withsinon
. Can you show how you importproductScope
in thejs/someClass
file?
– Dinesh Pandiyan
Nov 21 at 1:52
I don’t need to import it to the class because the class is kind of wrapped in it. Perhaps like how you’d use the $ as jQuery inside of an IIFE.
– Jaeeun Lee
Nov 21 at 5:33
Thanks, but still the same error, plus
1) "before each" hook for "should be a class"
message in red.– Jaeeun Lee
Nov 21 at 1:02
Thanks, but still the same error, plus
1) "before each" hook for "should be a class"
message in red.– Jaeeun Lee
Nov 21 at 1:02
Where are you getting
productScope
from? Are you importing it in your file or is it a global variable?– Dinesh Pandiyan
Nov 21 at 1:04
Where are you getting
productScope
from? Are you importing it in your file or is it a global variable?– Dinesh Pandiyan
Nov 21 at 1:04
Basically, there is a whole block of code that wraps the front end code, and it's a closure that captures
productScope
– Jaeeun Lee
Nov 21 at 1:06
Basically, there is a whole block of code that wraps the front end code, and it's a closure that captures
productScope
– Jaeeun Lee
Nov 21 at 1:06
If it's a closure, you could stub it with
sinon
. Can you show how you import productScope
in the js/someClass
file?– Dinesh Pandiyan
Nov 21 at 1:52
If it's a closure, you could stub it with
sinon
. Can you show how you import productScope
in the js/someClass
file?– Dinesh Pandiyan
Nov 21 at 1:52
I don’t need to import it to the class because the class is kind of wrapped in it. Perhaps like how you’d use the $ as jQuery inside of an IIFE.
– Jaeeun Lee
Nov 21 at 5:33
I don’t need to import it to the class because the class is kind of wrapped in it. Perhaps like how you’d use the $ as jQuery inside of an IIFE.
– Jaeeun Lee
Nov 21 at 5:33
|
show 2 more comments
You can try something like this
describe('#someClass', () => {
let someClass;
beforeEach(() => {
global.productScope = {
// mocking productScope object
};
});
it('should be a class', () => {
someClass = new SomeClass;
console.log(someClass);
});
afterEach(() => {
delete global.productScope;
});
});
or alternatively if you want more specific mock logic for each test case
describe('#someClass', () => {
let someClass;
it('should be a class', () => {
global.productScope = {
// mocking productScope object
};
// Test logic start
someClass = new SomeClass;
console.log(someClass);
// Test logic end
delete global.productScope;
});
});
add a comment |
You can try something like this
describe('#someClass', () => {
let someClass;
beforeEach(() => {
global.productScope = {
// mocking productScope object
};
});
it('should be a class', () => {
someClass = new SomeClass;
console.log(someClass);
});
afterEach(() => {
delete global.productScope;
});
});
or alternatively if you want more specific mock logic for each test case
describe('#someClass', () => {
let someClass;
it('should be a class', () => {
global.productScope = {
// mocking productScope object
};
// Test logic start
someClass = new SomeClass;
console.log(someClass);
// Test logic end
delete global.productScope;
});
});
add a comment |
You can try something like this
describe('#someClass', () => {
let someClass;
beforeEach(() => {
global.productScope = {
// mocking productScope object
};
});
it('should be a class', () => {
someClass = new SomeClass;
console.log(someClass);
});
afterEach(() => {
delete global.productScope;
});
});
or alternatively if you want more specific mock logic for each test case
describe('#someClass', () => {
let someClass;
it('should be a class', () => {
global.productScope = {
// mocking productScope object
};
// Test logic start
someClass = new SomeClass;
console.log(someClass);
// Test logic end
delete global.productScope;
});
});
You can try something like this
describe('#someClass', () => {
let someClass;
beforeEach(() => {
global.productScope = {
// mocking productScope object
};
});
it('should be a class', () => {
someClass = new SomeClass;
console.log(someClass);
});
afterEach(() => {
delete global.productScope;
});
});
or alternatively if you want more specific mock logic for each test case
describe('#someClass', () => {
let someClass;
it('should be a class', () => {
global.productScope = {
// mocking productScope object
};
// Test logic start
someClass = new SomeClass;
console.log(someClass);
// Test logic end
delete global.productScope;
});
});
answered Nov 29 at 13:17
elch_yan
46110
46110
add a comment |
add a comment |
I'm with other answers as well, as managing global
variables seems to be the simplest and most straightforward solution.
However, you can use toString to get class's string representation, and eval it to bind to closure's scope:
class someClass {
constructor() {
this.id = scopedId
}
}
// pass class as an argument
function scopeFactory(classDef) {
// define scoped data
let scopedId = 2;
// eval is used to bind class to the local closure
// so `scopedId` will be in charge
return eval("(" + classDef + ")");
}
const scopedSomeClass = scopeFactory(someClass);
console.log(new scopedSomeClass)
Note that eval(someCLass.toString())
doesn't work without parentheses.
You can add it as a helper function, into your project.
add a comment |
I'm with other answers as well, as managing global
variables seems to be the simplest and most straightforward solution.
However, you can use toString to get class's string representation, and eval it to bind to closure's scope:
class someClass {
constructor() {
this.id = scopedId
}
}
// pass class as an argument
function scopeFactory(classDef) {
// define scoped data
let scopedId = 2;
// eval is used to bind class to the local closure
// so `scopedId` will be in charge
return eval("(" + classDef + ")");
}
const scopedSomeClass = scopeFactory(someClass);
console.log(new scopedSomeClass)
Note that eval(someCLass.toString())
doesn't work without parentheses.
You can add it as a helper function, into your project.
add a comment |
I'm with other answers as well, as managing global
variables seems to be the simplest and most straightforward solution.
However, you can use toString to get class's string representation, and eval it to bind to closure's scope:
class someClass {
constructor() {
this.id = scopedId
}
}
// pass class as an argument
function scopeFactory(classDef) {
// define scoped data
let scopedId = 2;
// eval is used to bind class to the local closure
// so `scopedId` will be in charge
return eval("(" + classDef + ")");
}
const scopedSomeClass = scopeFactory(someClass);
console.log(new scopedSomeClass)
Note that eval(someCLass.toString())
doesn't work without parentheses.
You can add it as a helper function, into your project.
I'm with other answers as well, as managing global
variables seems to be the simplest and most straightforward solution.
However, you can use toString to get class's string representation, and eval it to bind to closure's scope:
class someClass {
constructor() {
this.id = scopedId
}
}
// pass class as an argument
function scopeFactory(classDef) {
// define scoped data
let scopedId = 2;
// eval is used to bind class to the local closure
// so `scopedId` will be in charge
return eval("(" + classDef + ")");
}
const scopedSomeClass = scopeFactory(someClass);
console.log(new scopedSomeClass)
Note that eval(someCLass.toString())
doesn't work without parentheses.
You can add it as a helper function, into your project.
class someClass {
constructor() {
this.id = scopedId
}
}
// pass class as an argument
function scopeFactory(classDef) {
// define scoped data
let scopedId = 2;
// eval is used to bind class to the local closure
// so `scopedId` will be in charge
return eval("(" + classDef + ")");
}
const scopedSomeClass = scopeFactory(someClass);
console.log(new scopedSomeClass)
class someClass {
constructor() {
this.id = scopedId
}
}
// pass class as an argument
function scopeFactory(classDef) {
// define scoped data
let scopedId = 2;
// eval is used to bind class to the local closure
// so `scopedId` will be in charge
return eval("(" + classDef + ")");
}
const scopedSomeClass = scopeFactory(someClass);
console.log(new scopedSomeClass)
edited Dec 4 at 7:42
answered Dec 2 at 21:51
Alex
3,130621
3,130621
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53398084%2funit-testing-es6-class-with-external-dependency%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
Please edit your question to include relevant parts of your unit test code
– Patrick Hund
Nov 20 at 17:18
Here is an answer that is in the style of your question: Read this: gofreerange.com/mocha/docs/Mocha/Mock.html
– Randy Casburn
Nov 20 at 17:25
@PatrickHund added some code
– Jaeeun Lee
Nov 20 at 17:39