Jest unit testing Advanced

  Jest instruction command window

  When learning portal Jest unit tests, we give Jest command provides a parameter --watchAll, it changes the listening test file test file or files introduced from time to time in order to be tested. But this also creates a problem, as long as the content changed little, Jest will put all tests are run again, a waste of resources. Is it possible to further optimize the --watchAll mode, it is there, in what place? In the command window. Execution npm run test, the test is completed, you will find many tips (Watch Usage), which is optimized for --watchAll mode

   Press f to run only failed tests. Press the f key to enter a model, only to run the test case failed. When we execute npm run test and found to have a test fails, then we want to run the failed test case, then press f it. Show you, just to change a test case error, such as the request of mock changed name: 'jason'

jest.mock('request', () => {
    return (url, callback) => {
        callback(null, 'ok', {name: 'jason'})
    }
});

   In this case again the test run again (watchAll mode), the command window shows the error, and displayed in the bottom press w to show more, while the cursor is flashing, waiting for input. Then press w, shows the contents of the image above, then f, ran only failed test, because three tests skipped, of course, there are certainly wrong, because we do not have to modify the test code

 

   In this case the right to modify the test code and save the test is successful, but it also ran just wrong test. At this point you modify func.test.js files or other test cases, the test will not be found to run, display No failed test found.

  Then we should withdraw from the f mode, Jest also prompted, press f exit 'only run failed test' mode. Then press f, returned watchAll mode, all the test cases to re-run it again.

  Use f mode is, npm run test has failed the test, according to f, modification failure to success, then f exit this mode.

  Press o to only run tests related to changed files. Press o, only changed files to run the test. Jest monitor file changes in real time, which if a file is changed, it will file in which the test run, no other changes in the test file, it will not run. That Jest is how to know which file is changed it? It did not know, need the help of git. Git is because the tracking file changes. As long as the code work area and a warehouse area of ​​contrast, we know which file to change the. That project should become git project. In the root directory, first built .gitignore file and then git init, the project becomes a git project, otherwise it will node_modules into git repository. At this time, for comparison, the test file func.test.js split into three, fetchData.test.js, forEach.test.js, math.test.js 

jest.mock('request', () => {
    return (url, callback) => {
        callback(null, 'ok', {name: 'sam'})
    }
});

const fetchData = require('./func').fetchData;

test('should return data when fetchData request success', () => {
   return fetchData().then(res => {
       expect(res).toEqual({name: 'sam'})
   })
})
const forEach = (array, callback) => {
    for (let index = 0; index < array.length; index++) {
        const element = array[index];
        callback(element);
    }
}

test('should call callback when forEach', () => {
    const mockFun = jest.fn(); // mock 函数
    const array = [1, 2];

    forEach (Array, mockFun); // instead of real callback function with mock 
    Expect (mockFun.mock.calls.length) .toBe ( 2 )
})
const math = require('./func');

test("calls math.add", () => {
  const addMock = jest.spyOn(math, "add");

  // override the implementation
  addMock.mockImplementation(() => "mock");
  expect(addMock(1, 2)).toEqual("mock");

  // restore the original implementation
  addMock.mockRestore();
  expect(addMock(1, 2)).toBeUndefined();
});

test('should call add', () => {
    function callMath(a, b) {
        return math.add(a + b);
    }
    const addMock = jest.spyOn(math, 'add');
    callMath(1, 2);
    the Expect (addMock) .toBeCalled ();   // toBeCalled, that function has not been called. 
})

  git add. and git commit -m "init git" to submit the file to the git repository. The next time you perform npm run test, start the test, press o, o into the mode, you can see the prompt, no file changes.

 

   Then change a file, such as forEach add a blank line, look at the console, only forEach.test.js test file is executed, the other did not perform the test file, and then change a file fetchData.test.js, two test files execution, or only changed files to run the test. At this reminds me of another parameter jest command, - watch, -o mode is not --watch it. The package.json in --watchAll into --watch 

"scripts": {
    "test": "jest --watch"
},

  Restart npm run test, with the -a mode, run all the tests, it is not that --watchAll, original --watch, --watchAll, a model, o mode, so when the line is interoperable.

 

   Look again at p, a test is performed in accordance with the file name, we provide a file name, it will only test the file, you can use regular expressions to match the file name. By p, prompted to enter the pattern, and then enter fetch, it will use the match to fetch all of the test file name, found fetchData.test.js test file, then it is executed. If you can not find any test file, it will not perform any tests.

 

   t test name is matched, there is a description of each test, the test's name can be called described. Provide a test name, it only ran this test, the same usage and p.

  q quit watch, enter a unit test is run, no matter in what mode, just press enter, it will run a pattern corresponding to the test.

Guess you like

Origin www.cnblogs.com/SamWeb/p/11826646.html