Best JavaScript code snippet using jest
globalTeardown.test.js
Source:globalTeardown.test.js
1/**2 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.3 *4 * This source code is licensed under the MIT license found in the5 * LICENSE file in the root directory of this source tree.6 * @flow7 */8'use strict';9import fs from 'fs';10import {createDirectory} from 'jest-util';11import os from 'os';12import path from 'path';13import runJest, {json as runWithJson} from '../runJest';14import {cleanup} from '../Utils';15const DIR = path.join(os.tmpdir(), 'jest-global-teardown');16const project1DIR = path.join(os.tmpdir(), 'jest-global-teardown-project-1');17const project2DIR = path.join(os.tmpdir(), 'jest-global-teardown-project-2');18beforeEach(() => {19 cleanup(DIR);20 cleanup(project1DIR);21 cleanup(project2DIR);22});23afterAll(() => {24 cleanup(DIR);25 cleanup(project1DIR);26 cleanup(project2DIR);27});28test('globalTeardown is triggered once after all test suites', () => {29 createDirectory(DIR);30 const teardownPath = path.resolve(31 __dirname,32 '../global-teardown/teardown.js',33 );34 const result = runWithJson('global-teardown', [35 `--globalTeardown=${teardownPath}`,36 `--testPathPattern=__tests__`,37 ]);38 expect(result.status).toBe(0);39 const files = fs.readdirSync(DIR);40 expect(files).toHaveLength(1);41 const teardown = fs.readFileSync(path.join(DIR, files[0]), 'utf8');42 expect(teardown).toBe('teardown');43});44test('jest throws an error when globalTeardown does not export a function', () => {45 const teardownPath = path.resolve(46 __dirname,47 '../global-teardown/invalidTeardown.js',48 );49 const {status, stderr} = runJest('global-teardown', [50 `--globalTeardown=${teardownPath}`,51 `--testPathPattern=__tests__`,52 ]);53 expect(status).toBe(1);54 expect(stderr).toMatch(55 `TypeError: globalTeardown file must export a function at ${teardownPath}`,56 );57});58test('globalTeardown function gets jest config object as a parameter', () => {59 const teardownPath = path.resolve(60 __dirname,61 '../global-teardown/teardownWithConfig.js',62 );63 const testPathPattern = 'pass';64 const result = runJest('global-teardown', [65 `--globalTeardown=${teardownPath}`,66 `--testPathPattern=${testPathPattern}`,67 ]);68 expect(result.stdout).toBe(testPathPattern);69});70test('should call globalTeardown function of multiple projects', () => {71 const configPath = path.resolve(72 __dirname,73 '../global-teardown/projects.jest.config.js',74 );75 const result = runWithJson('global-teardown', [`--config=${configPath}`]);76 expect(result.status).toBe(0);77 expect(fs.existsSync(DIR)).toBe(true);78 expect(fs.existsSync(project1DIR)).toBe(true);79 expect(fs.existsSync(project2DIR)).toBe(true);80});81test('should not call a globalTeardown of a project if there are no tests to run from this project', () => {82 const configPath = path.resolve(83 __dirname,84 '../global-teardown/projects.jest.config.js',85 );86 const result = runWithJson('global-teardown', [87 `--config=${configPath}`,88 '--testPathPattern=project-1',89 ]);90 expect(result.status).toBe(0);91 expect(fs.existsSync(DIR)).toBe(true);92 expect(fs.existsSync(project1DIR)).toBe(true);93 expect(fs.existsSync(project2DIR)).toBe(false);94});95test('globalTeardown works with default export', () => {96 const teardownPath = path.resolve(97 __dirname,98 '../global-teardown/teardownWithDefaultExport.js',99 );100 const testPathPattern = 'pass';101 const result = runJest('global-teardown', [102 `--globalTeardown=${teardownPath}`,103 `--testPathPattern=${testPathPattern}`,104 ]);105 expect(result.stdout).toBe(testPathPattern);106});107test('globalTeardown throws with named export', () => {108 const teardownPath = path.resolve(109 __dirname,110 '../global-teardown/invalidTeardownWithNamedExport.js',111 );112 const {status, stderr} = runJest('global-teardown', [113 `--globalTeardown=${teardownPath}`,114 `--testPathPattern=__tests__`,115 ]);116 expect(status).toBe(1);117 expect(stderr).toMatch(118 `TypeError: globalTeardown file must export a function at ${teardownPath}`,119 );...
invalidTeardownWithNamedExport.js
Source:invalidTeardownWithNamedExport.js
...3 *4 * This source code is licensed under the MIT license found in the5 * LICENSE file in the root directory of this source tree.6 */7function invalidTeardownWithNamedExport(jestConfig): void {8 console.log(jestConfig.testPathPattern);9}...
LambdaTest’s Jest Testing Tutorial covers step-by-step guides around Jest with code examples to help you be proficient with the Jest framework. The Jest tutorial has chapters to help you learn right from the basics of Jest framework to code-based tutorials around testing react apps with Jest, perform snapshot testing, import ES modules and more.
|<p>it('check_object_of_Car', () => {</p><p>
expect(newCar()).toBeInstanceOf(Car);</p><p>
});</p>|
| :- |
Get 100 minutes of automation test minutes FREE!!