简体   繁体   English

使用 Jest 的 mocking 值问题

[英]Issue with mocking values using Jest

I'm trying to setup Jest within an existing project that is already set up with enzyme & jasmine. I've installed the jest packages and have set up an initial configuration, but now I'm running into an issue where the mock data is not being provided correctly.我正在尝试在已经使用酶和 jasmine 设置的现有项目中设置 Jest。我已经安装了 jest 包并设置了初始配置,但现在我遇到了模拟数据的问题没有被正确提供。 The same test works fine in the project when run without jest.当没有开玩笑地运行时,相同的测试在项目中运行良好。

Here's the console output from the failing test:这是失败测试的控制台 output:

macbook-pro:$ ./node_modules/.bin/jest 
 FAIL  spec/cookieSpec.js
  Cookie Parser should
    ✓ return an empty string (17 ms)
    ✓ return null (2 ms)
    ✕ return the session value (2 ms)
    ✕ be able to handle multiple cookies (2 ms)
    ✕ be able to handle badly formatted cookies (1 ms)

  ● Cookie Parser should › return the session value

    expect(received).toMatch(expected)

    Expected substring: "4332432423"
    Received string:    ""

      24 |     const result = getSessionId();
      25 |     expect(result).not.toBe(null);
    > 26 |     expect(result).toMatch('4332432423');
         |                    ^
      27 |     done();
      28 |   });
      29 | 

      at Object.<anonymous> (spec/cookieSpec.js:26:20)

Jest configuration in package.json: package.json中的Jest配置:

"jest": {
    "verbose": true,
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "spec/mocks/*.js",
      "\\.(css|less)$": "spec/mocks/*.js"
    },
    "moduleDirectories": [
      "node_modules"
    ],
    "setupFilesAfterEnv": [
      "jest-enzyme"
    ],
    "transform": {
      "^.+\\.jsx?$": "babel-jest"
    },
    "testEnvironment": "enzyme",
    "testEnvironmentOptions": {
      "enzymeAdapter": "react16"
    },
    "testMatch": [
      "**/spec/*Spec.js?(x)"
    ]
  },

Code from the test file cookieSpec.js来自测试文件 cookieSpec.js 的代码

import {getSessionId} from 'Js/cookieReader.js';

describe('Cookie Parser should', function() {
  let _document = {cookie: ''};
  beforeEach(function() {
    global.document = _document;
  });

  it('return an empty string', function(done) {
    const result = getSessionId();
    expect(result).toBe('');
    done();
  });

  it('return null', function(done) {
    _document.cookie='ga=32213123';
    const result = getSessionId();
    expect(result).toBe('');
    done();
  });

  it('return the session value', function(done) {
    _document.cookie = 'connect.sid=4332432423';
    const result = getSessionId();
    expect(result).not.toBe(null);
    expect(result).toMatch('4332432423');
    done();
  });

  it('be able to handle multiple cookies', function(done) {
    _document.cookie='ga=32213123;connect.sid=4332432423';
    const result = getSessionId();
    expect(result).toBe('4332432423');
    done();
  });

  it('be able to handle badly formatted cookies', function(done) {
    _document.cookie=' ga=32213123;connect.sid= 4332432423';
    const result = getSessionId();
    expect(result).toBe('4332432423');
    done();
  });
});

It turns out that there was setup code that established the cookie mocks that was running for jasmine but not for jest.事实证明,有设置代码建立了运行 jasmine 但不是开玩笑的 cookie 模拟。 Once the mocks were established for jest, the tests passed.一旦为开玩笑建立了模拟,测试就通过了。

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

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