【问题标题】:Javascript unit testing errors, stubbed function still being calledJavascript单元测试错误,仍然调用存根函数
【发布时间】:2021-10-16 18:16:22
【问题描述】:

我正在尝试编写一些单元测试,但我在测试中遇到错误,我正在尝试了解错误发生的原因。

单元测试适用于调用features/index.ts 文件的index.ts 文件。我正在使用 sinon 将来自 features/index.ts 的默认导出存根。但是当我运行测试时,我收到以下错误TypeError: Cannot read property 'resolve' of undefined 指向文件features/feature1.ts

我已经从下面的测试和打字稿文件中添加了相关的摘录。

功能/feature1.ts
import path from "path";
import fs from "fs";
import {Setup} from "../types";

const TEMPLATE_ROOT = path.resolve(__dirname,"../../templates");
const INDEX_TEMPLATE = fs.readFileSync(TEMPLATE_ROOT, "index.js"), "utf8");

export const setup: Setup = async ({config, options}) => {
  // Internal code removed
}
特征/index.ts
import {setup as feature1} from "./feature1.ts";
import {setup as feature2} from "./feature2.ts";

type FeatureTypes = "feature1" | "feature2"

type Features = {
  [key in FeatureTypes]: Setup;
};

const features: Features = {
  feature1: feature1,
  feature2: feature2 
}

export default features
index.ts
import features from "./features"
import { Config, Options  } from "./types";

export async function init(config: Config, options: Options): Promise<void> {
  const nextFeature = options.features ? options.features.shift() : undefined;
  if (nextFeature) {

    // Other irrelevant code

    await Promise.resolve(features[nextFeature]({ config, options }));
    return init(config, options);
  }
}
index.spec.ts
import { expect } from "chai";
import * as sinon from "sinon";
import { init } from '.'
import * as features from "./features";
import { Config, Options  } from "./types"

describe("init", () => {
  const sandbox: sinon.SinonSandbox = sinon.createSandbox();
  let featuresStub: sinon.SinonStub;
  
  beforeEach(() => {
    featuresStub = sandbox.stub(features, "default").returns({
      feature1: sandbox.stub().resolves(),
      feature2: sandbox.stub().resolves(),
    });
  });

  afterEach(() => {
    sandbox.restore();
  });

  it("should call setup features", async () => {
    const setup: Setup = {
      features: [
        "feature1",
        "feature2",
      ],
    };

    await init({}, options);
    expect(featuresStub).to.have.been.calledOnce;
  });

  // rest of tests
});

我还尝试将存根设置更改为:

import * as feature1 from ".features/feature1";
import * as feature2 from ".features/feature2";

// Other code

describe("init", () => {
  const sandbox: sinon.SinonSandbox = sinon.createSandbox();
  let feature1Stub: sinon.SinonStub;
  let feature2Stub: sinon.SinonStub;
  
  beforeEach(() => {
    feature1Stub = sandbox.stub(feature1, "setup");
    feature2Stub = sandbox.stub(feature2, "setup");

    feature1Stub.resolves() 
    feature2Stub.resolves()
  });

  // Rest of code and tests
});

如果我对调用它的函数进行了存根处理,我不知道为什么它会尝试运行代码 const TEMPLATE_ROOT = path.resolve(__dirname,"../../templates");

【问题讨论】:

    标签: javascript typescript unit-testing sinon


    【解决方案1】:

    发现导入错误

    import path from "path";
    import fs from "fs";
    

    应该是:

    import * as path from "path";
    import * as fs from "fs";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 2022-11-30
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多