admin管理员组文章数量:1336645
When I try to test a typescript code:
namespace MainNamespace {
export class MainClass {
public sum(a: number, b: number) : number {
return a + b;
}
}
}
My test:
describe("main test", () => {
it("sum test", () => {
var mainClass = new MainNamespace.MainClass();
expect(mainClass.sum(3, 2)).toEqual(5);
})
})
I get the error:
ReferenceError: MainNamespace is not defined
How can I test the code with namespesaces with Jest (ts jest)?
When I try to test a typescript code:
namespace MainNamespace {
export class MainClass {
public sum(a: number, b: number) : number {
return a + b;
}
}
}
My test:
describe("main test", () => {
it("sum test", () => {
var mainClass = new MainNamespace.MainClass();
expect(mainClass.sum(3, 2)).toEqual(5);
})
})
I get the error:
ReferenceError: MainNamespace is not defined
How can I test the code with namespesaces with Jest (ts jest)?
Share Improve this question asked Apr 29, 2018 at 10:15 SvicerSvicer 2133 silver badges8 bronze badges 3- 3 Did you ever find a solution? I'm running into the same problem. – Bruno Finger Commented May 7, 2018 at 11:11
- I removed all the namespaces and now use only modules – Svicer Commented May 8, 2018 at 10:10
-
Hey, ing late to the party but running into a similar issue, I am using modules (something like
module MainModule { export class MainClass {} }
but I still get a syntax or type errors in my test files, did you useimport
of your source files in your test files or did you entirely rely on declaration merging ? – Akheloes Commented Aug 9, 2019 at 8:17
2 Answers
Reset to default 3Here is a working example:
index.ts
:
// tslint:disable-next-line: no-namespace
export namespace MainNamespace {
export class MainClass {
public sum(a: number, b: number): number {
return a + b;
}
}
}
index.spec.ts
:
import { MainNamespace } from './';
describe('MainNamespace', () => {
it('sum test', () => {
const mainClass = new MainNamespace.MainClass();
expect(mainClass.sum(3, 2)).toEqual(5);
});
});
Unit test result with 100% coverage:
PASS src/stackoverflow/50085505/index.spec.ts
MainNamespace
✓ sum test (7ms)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 5.713s, estimated 10s
Dependencies versions:
"typescript": "^3.6.4",
"jest": "^24.9.0",
"ts-jest": "^24.1.0",
Source code: https://github./mrdulin/jest-codelab/tree/master/src/stackoverflow/50085505
According to their docs, jest doesn't work with Typescript namespaces
本文标签: javascriptTesting typescript code with namespaces by jest (tsjest)Stack Overflow
版权声明:本文标题:javascript - Testing typescript code with namespaces by jest (ts-jest) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742407265a2469077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论