Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

和测试框架 jest 不兼容 #5

Open
myfjdthink opened this issue May 3, 2018 · 7 comments
Open

和测试框架 jest 不兼容 #5

myfjdthink opened this issue May 3, 2018 · 7 comments
Assignees

Comments

@myfjdthink
Copy link

myfjdthink commented May 3, 2018

准备一个测试 ModuleHook.test.js

const {hook} = require('module-hook')
const sinon = require('sinon')
const assert = require('assert')

describe('test/index.test.ts', () => {
  it('should loadModule work ok', done => {
    const spy = sinon.spy()
    hook('semver', '5.x', (loadModule, replaceSource, version) => {
      const semver = loadModule('semver.js');
      console.log('version', version)
      spy()
      assert(version)
      assert(semver);
    });
    require('semver');
    assert(spy.calledOnce);
    done()
  });
})

运行
mocha ModuleHook.test.js
没问题,测试通过

运行
jest ModuleHook.test.js
测试失败

    Received:
      false

      14 |     });
      15 |     require('semver');
    > 16 |     assert(spy.calledOnce);
      17 |     done()
      18 |   });
      19 | })

@mariodu

@czy88840616
Copy link
Member

jest 我们没用过。。得研究一下

@mariodu
Copy link
Member

mariodu commented May 3, 2018

这个是基于 mocha 的测试,jest 兼容吗?暂时没有迁移 jest 的计划

@mariodu
Copy link
Member

mariodu commented May 3, 2018

你是要自己写测试用例?然后用 jest 跑不过?

@mariodu
Copy link
Member

mariodu commented May 3, 2018

感觉应该是 jest 提前加载了模块,require 的 hook 没有检测到。具体还在看

@myfjdthink
Copy link
Author

@mariodu 是这样的

@myfjdthink
Copy link
Author

@mariodu 等待你的好消息,我也在看。

@mariodu
Copy link
Member

mariodu commented May 7, 2018

知道原因了,但是不太好弄。jest 有个默认的 resolver,提前把它加载了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants