简体   繁体   中英

Wallaby.js not working with jasmine callFake(…)?

Problem

wallaby.js seems not to be working with jasmine callFake . I want to use the arguments passed to the original function inside the the "fake" function. But I always get undefined with wallaby .

The test below works when running jasmine directly, but breaks when running via wallaby .

Did this happen with anyone else? Any ideas on how to fix it?

Test

it('test callFake and wallaby', async () => {
  // Arrange
  const myObj = {
    myFunc: (a) => a + 1,
  };

  spyOn(myObj, 'myFunc')
    .and.callFake(arg => arg);

  // Act
  const result = myObj.myFunc(1);

  // Assert
  expect(result).toBe(1);
});

在此处输入图片说明

Related info

Wallaby.js configuration file

module.exports = (wallaby) => {
  return {
    files: [
      'src/**/*.js',
      'migrations/*',
      'test/_helpers/*',
      'seeds/*',
      'config/*',
      { pattern: '.env', instrument: false },
    ],

    tests: [
      'test/**/*.spec.js',
    ],

    compilers: {
      '**/*.js': wallaby.compilers.babel(),
    },

    testFramework: 'jasmine',

    env: {
      type: 'node',

      params: {
        env: 'NODE_ENV=test;MONGODB_CONQUERY=mongodb://localhost:27017/athena-test',
      },
    },
    workers: {
      initial: 1,
      regular: 1,
      restart: true,
    },

    setup: (/* wallaby */) => {
      require('dotenv').load({ path: '.env' }); // eslint-disable-line
      require('./test/_helpers/dropDatabase'); // eslint-disable-line
    },

    teardown: (/* wallaby */) => {
    },
  };
};

Code editor or IDE name and version

Visual Studio Code v1.21.1

OS name and version

OSX 10.13.3

它是袋鼠的Jasmine 2.x支持中的一个错误, 现已修复

I found a workaround:

I used a reference to the spy inside the callFake function. See on the code below:

it('test callFake and wallaby', async () => {
    // Arrange
    const myObj = {
      myFunc: (a) => a + 1,
    };

    const spy = spyOn(myObj, 'myFunc')
      .and.callFake(
        () => spy.calls.argsFor(0)[0]
      );
    // Act
    const result = myObj.myFunc(1);

    // Assert
    expect(result).toBe(1);
  });

But I still think this is not the proper behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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