简体   繁体   English

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

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

I am trying to learn/understand how to do unit testing on typescript code and am running into an issue that I simply do not understand. 我试图学习/理解如何对打字稿代码进行单元测试,并且遇到了一个我根本不理解的问题。

If I import modules into my test.ts file, the tests will run fine but only pass if the result is NOT an object or array. 如果将模块导入到test.ts文件中,则测试可以正常运行,但只有在结果不是对象或数组的情况下才可以通过。 In those cases, the test will fail and tell me: 在这些情况下,测试将失败并告诉我:

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

Code: 码:

hello.ts: hello.ts:

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

test.ts: 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: 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 repo: https://github.com/llanginger/typescriptUnitTestsIssue github仓库: https : //github.com/llanginger/typescriptUnitTestsIssue

I imagine that there's something simple I'm missing here, but having worked through multiple examples of "typescript unit testing" tutorials using everything from a gulp suite to karma/sinon etc. The result is always the same - if I import a function that returns an object or array, the test will fail while presenting case-passing results. 我想这里缺少一些简单的东西,但是通过使用从gulp套件到karma / sinon等所有内容的“打字机单元测试”教程的多个示例进行了研究。结果始终是相同的-如果我导入了一个如果返回一个对象或数组,则在显示通过大小写的结果时测试将失败。 Any help would be much appreciated! 任何帮助将非常感激!

Since you're using expect you will want to user .deep to evaluate object equality: 由于您使用的expect ,你会想用户.deep评估对象的平等:

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

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

If you choose to also use assert with chai then you can use .deepEqual : 如果您还选择将assert与chai一起使用,则可以使用.deepEqual

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

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

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

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