<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [Testing] JavaScript Mocking Fundamentals

      Ensure Functions are Called Correctly with JavaScript Mocks

      Often when writing JavaScript tests and mocking dependencies, you’ll want to verify that the function was called correctly. That requires keeping track of how often the function was called and what arguments it was called with. That way we can make assertions on how many times it was called and ensure it was called with the right arguments.

       

      Function to be mocked: utils.js

      // returns the winning player or null for a tie
      // Let's pretend this isn't using Math.random() but instead
      // is making a call to some third party machine learning
      // service that has a testing environment we don't control
      // and is unreliable so we want to mock it out for tests.
      function getWinner(player1, player2) {
        const winningNumber = Math.random();
        return winningNumber < 1 / 3
          ? player1
          : winningNumber < 2 / 3
            ? player2
            : null;
      }
      
      module.exports = {getWinner};
      View Code

       

      Implementaion: thumbwar.js

      const utils = require("./utils");
      
      function thumbWar(player1, player2) {
        const numberToWin = 2;
        let player1Wins = 0;
        let player2Wins = 0;
        while (player1Wins < numberToWin && player2Wins < numberToWin) {
          const winner = utils.getWinner(player1, player2);
          if (winner === player1) {
            player1Wins++;
          } else if (winner === player2) {
            player2Wins++;
          }
        }
        return player1Wins > player2Wins ? player1 : player2;
      }
      
      module.exports = thumbWar;
      View Code

       

      Testing:

      const thumbWar = require("./thumbwar");
      const utils = require("./utils");
      const assert = require("assert");
      
      test("returns winner", () => {
      const originalGetWinner = utils.getWinner; utils.getWinner
      = jest.fn((p1, p2) => p1); // eslint-disable-line no-unused-vars const winner = thumbWar("KCD", "KW"); expect(winner).toBe("KCD"); // check the params are correct expect(utils.getWinner.mock.calls).toEqual([["KCD", "KW"], ["KCD", "KW"]]); // check the fn has been called number of times expect(utils.getWinner).toHaveBeenCalledTimes(2); // check each time call the fn with the correct params expect(utils.getWinner).toHaveBeenNthCalledWith(1, "KCD", "KW"); expect(utils.getWinner).toHaveBeenNthCalledWith(2, "KCD", "KW");

      utils.getWinner = originalGetWinner; });

      Here we are using 'jest.fn' to mock the function.

       

      We can also create a mock fn by ourselves.

      function fn(impl) {
        const mockFn = (...args) => {
          mockFn.mock.calls.push(args);
          return impl(...args);
        };
        mockFn.mock = {calls: []};
        return mockFn;
      }
      test("returns winner: fn", () => {
      const originalGetWinner = utils.getWinner; utils.getWinner
      = fn((p1, p2) => p1); // eslint-disable-line no-unused-vars const winner = thumbWar("KCD", "KW"); assert.strictEqual(winner, "KCD"); assert.deepStrictEqual(utils.getWinner.mock.calls, [ ["KCD", "KW"], ["KCD", "KW"], ]);
      utils.getWinner = originalGetWinner; });

       

       

      Restore the Original Implementation of a Mocked JavaScript Function with jest.spyOn

      With our current usage of the mock function we have to manually keep track of the original implementation so we can cleanup after ourselves to keep our tests idempotent (moonkey patching). Let’s see how jest.spyOn can help us avoid the bookkeeping and simplify our situation.

      test("returns winner", () => {
        //const originalGetWinner = utils.getWinner;
        //utils.getWinner = jest.fn((p1, p2) => p1); // eslint-disable-line no-unused-vars
        jest.spyOn(utils, "getWinner");
        utils.getWinner.mockImplementation((p1, p2) => p1); // eslint-disable-line no-unused-vars
        const winner = thumbWar("KCD", "KW");
        expect(winner).toBe("KCD");
        expect(utils.getWinner.mock.calls).toEqual([["KCD", "KW"], ["KCD", "KW"]]);
        expect(utils.getWinner).toHaveBeenCalledTimes(2);
        expect(utils.getWinner).toHaveBeenNthCalledWith(1, "KCD", "KW");
        expect(utils.getWinner).toHaveBeenNthCalledWith(2, "KCD", "KW");
      
        // utils.getWinner = originalGetWinner;
        utils.getWinner.mockRestore();
      });

      Here we are using jest.spyOn function.

      We can also write spyOn function by ourselves.

      function fn(impl = () => {}) {
        const mockFn = (...args) => {
          mockFn.mock.calls.push(args);
          mockFn.mockImplementation = newImpl => (impl = newImpl);
          return impl(...args);
        };
        mockFn.mock = {calls: []};
        return mockFn;
      }
      
      function spyOn(obj, prop) {
        // store the origianl fn
        const originalValue = obj[prop];
        // assign new mock fn
        obj[prop] = fn;
        // add restore fn
        obj[prop].mockRestore = () => (obj[prop] = originalValue);
      }
      
      test("returns winner: fn", () => {
        spyOn(utils, "getWinner");
        utils.getWinner.mockImplementation = fn((p1, p2) => p1); // eslint-disable-line no-unused-vars
        const winner = thumbWar("KCD", "KW");
        assert.strictEqual(winner, "KCD");
        assert.deepStrictEqual(utils.getWinner.mock.calls, [
          ["KCD", "KW"],
          ["KCD", "KW"],
        ]);
        utils.getWinner.mockRestore();
      });

       

      Mock a JavaScript module in a test

      So far we’re still basically monkey-patching the utils module which is fine, but could lead to problems in the future, especially if we want to mock a ESModule export which doesn’t allow this kind of monkey-patching on exports. Instead, let’s mock the entire module so when our test subject requires the file they get our mocked version instead.

      To mock a whole module. we can use 'jest.mock':

      const thumbWar = require("./thumbwar");
      const utils = require("./utils");
      const assert = require("assert");
      
      jest.mock("./utils", () => {
        return {
          getWinner: jest.fn((p1, p2) => p1), // eslint-disable-line no-unused-vars
        };
      });
      
      test("returns winner", () => {
      
        const winner = thumbWar("KCD", "KW");
        expect(winner).toBe("KCD");
        expect(utils.getWinner.mock.calls).toEqual([["KCD", "KW"], ["KCD", "KW"]]);
        expect(utils.getWinner).toHaveBeenCalledTimes(2);
        expect(utils.getWinner).toHaveBeenNthCalledWith(1, "KCD", "KW");
        expect(utils.getWinner).toHaveBeenNthCalledWith(2, "KCD", "KW");
      
        utils.getWinner.mockReset();
      });

      Now we don't need to mock the 'getWinner' function inside test, 'jest.mock' can be used anywhere, jest will make sure it mock will be hoisted to the top.

       

      Make a shared JavaScript mock module

      Often you’ll want to mock the same file throughout all the tests in your codebase. So let’s make a shared mock file in Jest's __mocks__ directory which Jest can load for us automatically.

      __mocks__/utils.js:

      module.exports = {
        getWinner: jest.fn((p1, p2) => p1), // eslint-disable-line no-unused-vars
      };

       

      const thumbWar = require("../thumbwar");
      const utils = require("../utils");
      const assert = require("assert");
      
      jest.mock("../utils");
      
      test("returns winner", () => {
        const winner = thumbWar("KCD", "KW");
        expect(winner).toBe("KCD");
        expect(utils.getWinner.mock.calls).toEqual([["KCD", "KW"], ["KCD", "KW"]]);
        expect(utils.getWinner).toHaveBeenCalledTimes(2);
        expect(utils.getWinner).toHaveBeenNthCalledWith(1, "KCD", "KW");
      expect(utils.getWinner).toHaveBeenNthCalledWith(
      2, "KCD", "KW"); utils.getWinner.mockReset(); });

       

      posted @ 2018-11-06 04:25  Zhentiw  閱讀(437)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 久久综合九色综合久桃花| 国产乱xxxxx97国语对白| 色综合 图片区 小说区| 南投县| 91偷自国产一区二区三区| 中文字幕人妻无码一夲道| 中文字幕无线码在线观看| 少妇无套内谢免费视频| 天堂亚洲免费视频| 国产一区二区三区四区五区加勒比| 成人做爰www网站视频| 啊轻点灬大JI巴太粗太长了在线| 天天躁夜夜踩很很踩2022| 国产粉嫩美女一区二区三| 高清自拍亚洲精品二区| 蜜桃无码一区二区三区| 亚洲午夜福利网在线观看 | 亚洲欧美日韩精品成人| 国产黄色看三级三级三级| 国产成人精品亚洲资源| 精品日韩精品国产另类专区| 中文字幕av国产精品| 人妻少妇偷人无码视频| 国产精成人品日日拍夜夜| 欧美日产国产精品日产| 国产日韩AV免费无码一区二区三区| 丰满多毛的大隂户视频| 99中文字幕国产精品| 国产成人亚洲无码淙合青草| 日本熟妇hdsex视频| 亚洲女同在线播放一区二区| 国产婷婷色一区二区三区| 亚洲悠悠色综合中文字幕| 亚洲小说乱欧美另类| 欧美熟妇乱子伦XX视频| 亚洲黄色片一区二区三区| 中文字幕在线视频不卡一区二区| 亚洲综合国产成人丁香五| 成人午夜在线观看日韩| 亚洲成人av在线资源| 伊人久久大香线蕉综合网站|