简体   繁体   English

Jest 找不到模块

[英]Jest can't find module

I am just starting out with Jest, and I am trying to test this code that will change the textContent of an element after 1000ms:我刚开始使用 Jest,我正在尝试测试此代码,该代码将在 1000 毫秒后更改元素的 textContent:

 const subtext = document.querySelector('.subtext');

 function delayChangeText() {
    setTimeout(() => {
        subtext.textContent = "Dev";
    }, 1000);
}

subtext.addEventListener('load', delayChangeText);

This is what Jest returns:这是 Jest 返回的内容:

FAIL  js/app.test.js
  ● Test suite failed to run

Cannot find module './delayChangeText' from 'js/app.test.js'

> 1 | const delayChangeText = require('./delayChangeText');
    | ^
  2 |
  3 | test('Change the text after 1000 seconds', () => {
  4 |     expect(delayChangeText().toBe(subtext.textContent = "Dev"));
  at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:311:11)
  at Object.<anonymous> (js/app.test.js:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.846 s

I am still pretty new to testing, I'm confident I made a pretty simple goof.我对测试还是很陌生,我相信我做了一个非常简单的傻事。 Any help is much appreciated.任何帮助深表感谢。 Best regards.最好的祝福。

The zeroth rule of testing is:测试的第零规则是:

Code must be written such that it is testable代码必须编写成可测试的

Not all code can be tested.并非所有代码都可以测试。 Sometimes you have to change how your real code is written so that a testing framework can get its hands on the code.有时,您必须更改实际代码的编写方式,以便测试框架可以掌握代码。

I can see one or two critical problems.我可以看到一两个关键问题。

First: I assume you didn't include the full content of your application, but it does not look like your app code exports the delayChangeText function, which means that other modules (such as your test suite) can't import it.第一:我假设您没有包含应用程序的全部内容,但看起来您的应用程序代码没有导出delayChangeText函数,这意味着其他模块(例如您的测试套件)无法导入它。

You may need to do something like module.exports = delayChangeText , or export default delayChangeText in your app code.您可能需要执行类似module.exports = delayChangeText ,或者在您的应用程序代码中export default delayChangeText

Second: your function is not a pure function.第二:你的函数不是纯函数。 That is, it depends on stuff that's not passed to it explicitly, namely it expects that subtext is defined within its execution context.也就是说,这取决于这不是传递给它明确的东西,即它预期subtext是它的执行上下文中定义。

It's not strictly required that all your functions be pure functions, and indeed sometimes it's not possible.并不是严格要求您的所有函数都是纯函数,实际上有时这是不可能的。 But pure functions are usually much easier to test (as well as being easier to design and implement).但是纯函数通常更容易测试(也更容易设计和实现)。 Here's a pure version of your function:这是您的函数的纯版本:

function delayChangeText(element) {
    setTimeout(() => {
        element.textContent = "Dev";
    }, 1000);
}

You don't have to convert this to a pure function, but your code will break in the test unless your test suite takes steps to ensure that subtext.textContent doesn't throw -- if subtext is undefined, it will throw.您不必将其转换为一个纯粹的功能,但除非你的测试套件采取措施,以确保您的代码将在测试中打破subtext.textContent不会抛出-如果subtext是不明确的,它会抛出。

This is important for another reason: if this module's default export is the delayChangeText function, then it's probably not appropriate for the preceding subtext assignment to even be in the file.这是另一个重要原因:如果这个模块的默认出口是delayChangeText功能,那么它可能不适合前面的subtext分配即使是在文件中。 Which means that fixing the first problem ("it's not being exported") will naturally result in converting the function to a pure function.这意味着解决第一个问题(“它没有被导出”)自然会导致将函数转换为纯函数。 If you really want to avoid that, you can: you'll probably have to change how the function is imported in the test suite, to this:如果您真的想避免这种情况,您可以:您可能必须将函数在测试套件中的导入方式更改为:

const { delayChangeText } = require('./delayChangeText');

Finally (and you didn't ask about this -- yet ): you probably don't want this test to have to actually wait 1000 ms to test this function.最后(并且您没有问这个问题——还没有):您可能不希望此测试实际上必须等待 1000 毫秒来测试此功能。 Jest has some tools for manipulating the global timer functions , which will allow you to validate the function without having to wait. Jest 有一些用于操作全局计时器函数的工具,这将使您无需等待即可验证该函数。

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

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