Best JavaScript code snippet using jest
globalSetup.test.js
Source:globalSetup.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 os from 'os';11import path from 'path';12import runJest, {json as runWithJson} from '../runJest';13import {cleanup} from '../Utils';14const DIR = path.join(os.tmpdir(), 'jest-global-setup');15const project1DIR = path.join(os.tmpdir(), 'jest-global-setup-project-1');16const project2DIR = path.join(os.tmpdir(), 'jest-global-setup-project-2');17const customTransformDIR = path.join(18 os.tmpdir(),19 'jest-global-setup-custom-transform',20);21beforeEach(() => {22 cleanup(DIR);23 cleanup(project1DIR);24 cleanup(project2DIR);25 cleanup(customTransformDIR);26});27afterAll(() => {28 cleanup(DIR);29 cleanup(project1DIR);30 cleanup(project2DIR);31 cleanup(customTransformDIR);32});33test('globalSetup is triggered once before all test suites', () => {34 const setupPath = path.resolve(__dirname, '../global-setup/setup.js');35 const result = runWithJson('global-setup', [36 `--globalSetup=${setupPath}`,37 `--testPathPattern=__tests__`,38 ]);39 expect(result.status).toBe(0);40 const files = fs.readdirSync(DIR);41 expect(files).toHaveLength(1);42 const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8');43 expect(setup).toBe('setup');44});45test('jest throws an error when globalSetup does not export a function', () => {46 const setupPath = path.resolve(__dirname, '../global-setup/invalidSetup.js');47 const {status, stderr} = runJest('global-setup', [48 `--globalSetup=${setupPath}`,49 `--testPathPattern=__tests__`,50 ]);51 expect(status).toBe(1);52 expect(stderr).toMatch(53 `TypeError: globalSetup file must export a function at ${setupPath}`,54 );55});56test('globalSetup function gets jest config object as a parameter', () => {57 const setupPath = path.resolve(58 __dirname,59 '../global-setup/setupWithConfig.js',60 );61 const testPathPattern = 'pass';62 const result = runJest('global-setup', [63 `--globalSetup=${setupPath}`,64 `--testPathPattern=${testPathPattern}`,65 ]);66 expect(result.stdout).toBe(testPathPattern);67});68test('should call globalSetup function of multiple projects', () => {69 const configPath = path.resolve(70 __dirname,71 '../global-setup/projects.jest.config.js',72 );73 const result = runWithJson('global-setup', [`--config=${configPath}`]);74 expect(result.status).toBe(0);75 expect(fs.existsSync(DIR)).toBe(true);76 expect(fs.existsSync(project1DIR)).toBe(true);77 expect(fs.existsSync(project2DIR)).toBe(true);78});79test('should not call a globalSetup of a project if there are no tests to run from this project', () => {80 const configPath = path.resolve(81 __dirname,82 '../global-setup/projects.jest.config.js',83 );84 const result = runWithJson('global-setup', [85 `--config=${configPath}`,86 '--testPathPattern=project-1',87 ]);88 expect(result.status).toBe(0);89 expect(fs.existsSync(DIR)).toBe(true);90 expect(fs.existsSync(project1DIR)).toBe(true);91 expect(fs.existsSync(project2DIR)).toBe(false);92});93test('should not call any globalSetup if there are no tests to run', () => {94 const configPath = path.resolve(95 __dirname,96 '../global-setup/projects.jest.config.js',97 );98 const result = runWithJson('global-setup', [99 `--config=${configPath}`,100 // onlyChanged ensures there are no tests to run101 '--onlyChanged',102 ]);103 expect(result.status).toBe(0);104 expect(fs.existsSync(DIR)).toBe(false);105 expect(fs.existsSync(project1DIR)).toBe(false);106 expect(fs.existsSync(project2DIR)).toBe(false);107});108test('globalSetup works with default export', () => {109 const setupPath = path.resolve(110 __dirname,111 '../global-setup/setupWithDefaultExport.js',112 );113 const testPathPattern = 'pass';114 const result = runJest('global-setup', [115 `--globalSetup=${setupPath}`,116 `--testPathPattern=${testPathPattern}`,117 ]);118 expect(result.stdout).toBe(testPathPattern);119});120test('globalSetup throws with named export', () => {121 const setupPath = path.resolve(122 __dirname,123 '../global-setup/invalidSetupWithNamedExport.js',124 );125 const {status, stderr} = runJest('global-setup', [126 `--globalSetup=${setupPath}`,127 `--testPathPattern=__tests__`,128 ]);129 expect(status).toBe(1);130 expect(stderr).toMatch(131 `TypeError: globalSetup file must export a function at ${setupPath}`,132 );133});134test('should not transpile the transformer', () => {135 const {status} = runJest('global-setup-custom-transform', [`--no-cache`]);136 expect(status).toBe(0);...
invalidSetupWithNamedExport.js
Source:invalidSetupWithNamedExport.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 invalidSetupWithNamedExport(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!!