nodejs项目graphql中的Jest环境已被拆 debugging 误,使用jest.useFakeTimers,但仍然得到错误

rxztt3cl  于 2023-08-01  发布在  Jest
关注(0)|答案(1)|浏览(128)

获取引用错误:您正在尝试在Jest环境被拆除后import一个文件。Used jest.useFakeTimers();进口后,但问题仍然存在。有什么办法解决吗?
下面是我的jest.config.ts

import type { Config } from "@jest/types"
const config: Config.InitialOptions = {
    transform: {
        '^.+\\.tsx?$': 'ts-jest'
    },
    testRegex: '(/__test__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
    moduleFileExtensions: [ 'js','ts'],
    testEnvironment: "jest-environment-node",
    fakeTimers: {
        enableGlobally: true,
      },
    verbose: true
}
export default config

字符串
测试文件office.test.ts

import {ApolloServer} from 'apollo-server'
import {expect,test,jest} from '@jest/globals';
import officeGql  from '../controllers/controller'
jest.useFakeTimers();
test('Office Module Test', async () => {
  let typeDefs = officeGql.types
  let resolvers = officeGql.resolvers
  let testServer = new ApolloServer({
    typeDefs,
    resolvers,
  });
   const response = await testServer.executeOperation({
     query: `query offices(limit: $limit,offset: $offset){name, address}`,
       variables: { limit: 10,offset:0 },
   });
   expect(response.data).toBeTruthy();
});

7cwmlq89

7cwmlq891#

该问题是一个开放句柄。要解决这个问题,你所要做的就是

try {
let typeDefs = officeGql.types
  let resolvers = officeGql.resolvers
  let testServer = new ApolloServer({
    typeDefs,
    resolvers,
  });
   const response = await testServer.executeOperation({
     query: `query offices(limit: $limit,offset: $offset){name, address}`,
       variables: { limit: 10,offset:0 },
   });
   expect(response.data).toBeTruthy();
} catch (err) {
} finally {
  await testServer.close()
}

字符串
这是一个临时修复,将工作时,我找到一个更好的方式,我会张贴它

相关问题