繁体   English   中英

仅在结果不是对象/数组的情况下,才能对导入的模块进行打字稿单元测试

[英]Typescript Unit Tests on imported modules only works if result is not object/array

我试图学习/理解如何对打字稿代码进行单元测试,并且遇到了一个我根本不理解的问题。

如果将模块导入到test.ts文件中,则测试可以正常运行,但只有在结果不是对象或数组的情况下才可以通过。 在这些情况下,测试将失败并告诉我:

AssertionError: expected { greeting: 'Hello' } to equal { greeting: 'Hello' }

码:

hello.ts:

 export function helloString() { return "Hello"; } export function helloObject() { return {greeting: "Hello"} } export function helloArray() { return ["Hello"] } 

test.ts:

 import { helloString, helloObject, helloArray } from "./hello"; import { expect } from "chai" describe("Hello string function", () => { it("should return hello", () => { const result = helloString(); expect(result).to.equal("Hello"); }) }) describe("Hello object function", () => { it("should return hello", () => { const result = helloObject(); expect(result).to.equal({greeting: "Hello"}); }) }) describe("Hello array function", () => { it("should return hello", () => { const result = helloArray(); expect(result).to.equal(["Hello"]); }) }) 

package.json:

 { "name": "typescriptTesting", "version": "1.0.0", "main": "index.js", "license": "MIT", "scripts": { "test": "mocha -r ts-node/register src/test.ts" }, "devDependencies": { "@types/chai": "^3.4.35", "@types/mocha": "^2.2.39", "chai": "^3.5.0", "mocha": "^3.2.0", "ts-node": "^2.1.0", "typescript": "^2.2.1" } } 

github仓库: https : //github.com/llanginger/typescriptUnitTestsIssue

我想这里缺少一些简单的东西,但是通过使用从gulp套件到karma / sinon等所有内容的“打字机单元测试”教程的多个示例进行了研究。结果始终是相同的-如果我导入了一个如果返回一个对象或数组,则在显示通过大小写的结果时测试将失败。 任何帮助将非常感激!

由于您使用的expect ,你会想用户.deep评估对象的平等:

expect(foo).to.deep.equal({ bar: 'baz' });

链接: http//chaijs.com/api/bdd/#method_deep

如果您还选择将assert与chai一起使用,则可以使用.deepEqual

assert.deepEqual({ tea: 'green' }, { tea: 'green' });

链接: http//chaijs.com/api/assert/#method_deepequal

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM