【问题标题】:Reusing Jest unit tests重用 Jest 单元测试
【发布时间】:2018-12-24 13:01:58
【问题描述】:

我正在尝试使用 Jest 测试几个数据库实现。为了帮助测试这些实现,我首先提出了一组针对 API 的单元测试,这两种实现都应该实现。

我目前正在努力将这两个实现传递给测试套件。

以下是最简单形式的(虚拟)MongoDB 实现:

class MongoDB {
  async query () {
    console.warn(`This is a dummy function.`)
  }

  async connect () {
    // The real connect takes some time..instead we just simulate it
    await new Promise((resolve, reject) => {
      setTimeout(resolve, 300)
    })
  }
}

这是我测试的一个小sn-p:

let db
beforeAll(async () => {
  db = new MongoDB()
  await db.connect()
  console.log(`mongoDB ready`)
})

async function testDB (db) {
  describe('Basic', async () => {
    test('Valid instance', async () => {
      expect(db).toBeTruthy()
      expect(db.query).toBeTruthy()
    })
  })
}

describe('Tests', async () => {
  console.log(`Running testDB`)
  testDB(db) // Have also unsuccessfully tried changing this to: return testDB(db)
})

我使用这种方法的目标是将我的所有测试包装在 testDB 函数中,并使用各种实现简单地调用它。比如testDB(new MongoDB())和testDB(new MemoryDB())等等。

但是,这似乎没有按预期工作。 上面的代码导致一个错误,指出:

  ● Tests › Basic › Valid instance

    expect(received).toBeTruthy()

    Expected value to be truthy, instead received
      undefined

console.log 语句的顺序似乎表明测试正在运行之前 db 被初始化。

  console.log mongo.test.js:20
    Running testDB

  console.log mongo.test.js:7
    mongoDB ready

整个示例以及生成的输出可以是reproduced on repl.it。

我怎样才能重用单元测试来测试多个实现,而不需要重复测试和维护两个版本?

【问题讨论】:

    标签: javascript unit-testing jestjs


    【解决方案1】:

    今天面临同样的需求。这是改编自打字稿的方式,但您明白了:

    // common/service.test.js
    export const commonServiceTests = (name, impl) => {
      describe(`Common tests for ${implName}`, () => {
        // pile your tests here
        test('test1', () => { ... });
        test('test2', () => { ... });
        test('test3', () => { ... });
      });
    }
    
    // just to avoid warning, that no tests in test file
    describe('Common tests for CommonService implementations', () => {
      test('should be used per implementation', () => {});
    });
    

    对于您的每个实施:

    // inmemory/service.test.js
    import { commonServiceTests } from '../common/service.test';
    import ...; // your implementation here
    
    const myInMemoryService = ...; // initialize it
    
    commonServiceTests('InMemory', myInMemoryService);
    

    那么common/service.test.js中定义的所有测试都将在每个实现测试中执行。

    如果您的初始化是async(很可能),那么您的共享测试也应该是async。那么:

    // common/service.test.js
    export const commonServiceTests = (name, impl: Promise) => {
      describe(`Common tests for ${implName}`, () => {
        // pile your async tests here
        test('test1', async () => {
          const svc = await impl;
          return await svc.doSomthingPromisy();
        });
        test('test2', () => { ... });
        test('test3', () => { ... });
      });
    }
    

    【讨论】:

    • 我使用了这个确切的流程,它与jest 一起工作得很好,但是一旦我们尝试将此方法与playwright-test 一起用于测试注释(跳过、慢速等),它就会挂起。
    猜你喜欢
    • 2018-12-16
    • 2020-08-04
    • 2022-01-06
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多