This might be a hoisting issue. If you have more JS code above your code snippet, test might have been assigned to something else.
This will cause a TypeError because test will be a string by the time you call it:
// ... some code
var test = 'This is a test.';
// ... more code
function test() {
console.log(this);
}
test();
This will work because test is assigned to a function right before you call it:
// ... some code
var test = 'This is a test.';
// ... more code
test = function () {
console.log(this);
}
test();
In the first example, this is what the interpreter does, more or less:
- Assign a function definition to
test. (This happens first because it hoists function declarations above everything else) - Reassign
testto be a string. - Invoke
test, which is a string.
Second example:
- Declare
testwith no value (undefined). - Assign
testto a string. - Reassign
testto a function. - Invoke
test, which is a function.
This might be a hoisting issue. If you have more JS code above your code snippet, test might have been assigned to something else.
This will cause a TypeError because test will be a string by the time you call it:
// ... some code
var test = 'This is a test.';
// ... more code
function test() {
console.log(this);
}
test();
This will work because test is assigned to a function right before you call it:
// ... some code
var test = 'This is a test.';
// ... more code
test = function () {
console.log(this);
}
test();
In the first example, this is what the interpreter does, more or less:
- Assign a function definition to
test. (This happens first because it hoists function declarations above everything else) - Reassign
testto be a string. - Invoke
test, which is a string.
Second example:
- Declare
testwith no value (undefined). - Assign
testto a string. - Reassign
testto a function. - Invoke
test, which is a function.
I just did a CTRL+F on your code and it seems that you already have a variable defined with the name of test in above scope. Please edit your code to remove the test variable declared above
TypeError: test.describe is not a function when using nodejs, selenium and mocha
react - TypeError: tests is not a function Error in Truffle - Ethereum Stack Exchange
JavaScript `.test()` is not a function
TypeError: t.test is not a function
I'm getting the error TypeError: is not a function for several Typescript methods I'm testing using mocha. All the importing/exporting for the mocha test seems to be working fine. I'm receiving the error for the specific methods referenced in the code in my StackOverflow post.
https://stackoverflow.com/questions/63064952/typescript-mocha-testing-how-to-solve-typeerror-is-not-a-function
Any advice would be greatly appreciated!
In my case, I had to mock a Node.js module. I'm using React and Redux in ES6, with Jest and Enzyme for unit tests.
In the file I'm using, and writing a test for, I'm importing the node modules as default:
import nodeModulePackage from 'nodeModulePackage';
So I needed to mock it as a default since I kept getting the error (0, _blah.default) is not a function..
My solution was to do:
jest.mock('nodeModulePackage', () => jest.fn(() => {}));
In my case, I just needed to override the function and make it return an empty object.
If you need to call a function on that node module, you'll do the following:
jest.mock('nodeModulePackage', () => ({ doSomething: jest.fn(() => 'foo') }));
I figured this out. It is to do with hoisting, see: Jest mocking reference error
The reason it had worked in a previous test, where I had done it, was because the testSubject was itself a class. This meant that when the testSubject was instantiated, it was after the variable declaration in the test file, so the mock had access to use it.
So in the above case it was never going to work.
Your pattern must be RegEx literal (double quotes around that should not be there) like this
var pattern = /^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/;
Otherwise you need to use RegExp object, with proper escaping for \, like this
var pattern = new RegExp("^(()?\\d{3}())?(-|\\s)?\\d{3}(-|\\s)?\\d{4}$");
Your pattern holds a string, and not a regular expression.
Make it var pattern = /^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/; (without quotes)
class MockPartservice {
list():Obserable<Part>{
let part1:Part;
part1=new Part();
part1.description="This is a test data";
part1.name="Hello";
part1.uuid="ABCD1234";
return of(part1);
}
};
You need to return a Observable and return data using of() operator.And both need to be import from 'rxjs'
and you don't need to use spyOn
After fixture creation call fixture.detectChanges();
fixture = TestBed.createComponent(ComponentName);
component = fixture.componentInstance;
//call detect changes here
fixture.detectChanges();