From 0c6f35fe56dc0550b70a14d2b18dee8a3436e3e7 Mon Sep 17 00:00:00 2001 From: Aaron Abramov Date: Thu, 21 Jun 2018 13:28:42 -0700 Subject: [PATCH] sync jest dir --- jest/__atom_tests__/builtin-modules-test.js | 18 +++++++++++ jest/__atom_tests__/fake_timers-test.js | 16 +++++++++ jest/__atom_tests__/sample-1-test.js | 14 ++++++++ jest/__atom_tests__/sample-2-test.js | 14 ++++++++ jest/__mocks__/emptyObject.js | 13 ++++++++ jest/__mocks__/log4js.js | 36 +++++++++++++++++++++ jest/__mocks__/nuclide-commons/analytics.js | 35 ++++++++++++++++++++ jest/__tests__/waits_for-test.js | 28 ++++++++++++++++ jest/build_atom_jest_runner.sh | 15 +++++++++ jest/jest.config.atom.js | 36 +++++++++++++++++++++ jest/jest.config.node.js | 31 ++++++++++++++++++ jest/setup.js | 15 +++++++++ jest/setupTestFrameworkScriptFile.atom.js | 17 ++++++++++ jest/setupTestFrameworkScriptFile.node.js | 12 +++++++ jest/waits_for.js | 33 +++++++++++++++++++ scripts/sync-nuclide.sh | 1 + 16 files changed, 334 insertions(+) create mode 100644 jest/__atom_tests__/builtin-modules-test.js create mode 100644 jest/__atom_tests__/fake_timers-test.js create mode 100644 jest/__atom_tests__/sample-1-test.js create mode 100644 jest/__atom_tests__/sample-2-test.js create mode 100644 jest/__mocks__/emptyObject.js create mode 100644 jest/__mocks__/log4js.js create mode 100644 jest/__mocks__/nuclide-commons/analytics.js create mode 100644 jest/__tests__/waits_for-test.js create mode 100755 jest/build_atom_jest_runner.sh create mode 100644 jest/jest.config.atom.js create mode 100644 jest/jest.config.node.js create mode 100644 jest/setup.js create mode 100644 jest/setupTestFrameworkScriptFile.atom.js create mode 100644 jest/setupTestFrameworkScriptFile.node.js create mode 100644 jest/waits_for.js diff --git a/jest/__atom_tests__/builtin-modules-test.js b/jest/__atom_tests__/builtin-modules-test.js new file mode 100644 index 00000000..3a85cde1 --- /dev/null +++ b/jest/__atom_tests__/builtin-modules-test.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +test('pass', () => { + const atom = require('atom'); + const electron = require('electron'); + + expect(atom).toBeDefined(); + expect(electron).toBeDefined(); +}); diff --git a/jest/__atom_tests__/fake_timers-test.js b/jest/__atom_tests__/fake_timers-test.js new file mode 100644 index 00000000..0cf03eaa --- /dev/null +++ b/jest/__atom_tests__/fake_timers-test.js @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +test('fake timers are not available yet', () => { + expect(() => { + jest.useFakeTimers(); + }).toThrow('fakeTimers are not supproted in atom environment'); +}); diff --git a/jest/__atom_tests__/sample-1-test.js b/jest/__atom_tests__/sample-1-test.js new file mode 100644 index 00000000..ca717f8a --- /dev/null +++ b/jest/__atom_tests__/sample-1-test.js @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @flow strict + * @format + */ + +test('atom', () => { + expect(1).toBe(1); +}); diff --git a/jest/__atom_tests__/sample-2-test.js b/jest/__atom_tests__/sample-2-test.js new file mode 100644 index 00000000..ddfedf42 --- /dev/null +++ b/jest/__atom_tests__/sample-2-test.js @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @flow strict + * @format + */ + +test('atom', () => { + expect(2).toBe(2); +}); diff --git a/jest/__mocks__/emptyObject.js b/jest/__mocks__/emptyObject.js new file mode 100644 index 00000000..6978cae6 --- /dev/null +++ b/jest/__mocks__/emptyObject.js @@ -0,0 +1,13 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @flow strict + * @format + */ + +// eslint-disable-next-line nuclide-internal/no-commonjs +module.exports = {}; diff --git a/jest/__mocks__/log4js.js b/jest/__mocks__/log4js.js new file mode 100644 index 00000000..a8314406 --- /dev/null +++ b/jest/__mocks__/log4js.js @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @flow strict + * @format + */ + +const logger: log4js$Logger = { + debug: jest.fn(), + error: jest.fn(), + fatal: jest.fn(), + info: jest.fn(), + isLevelEnabled: jest.fn(), + // $FlowFixMe + level: jest.fn(), + log: jest.fn(), + mark: jest.fn(), + removeLevel: jest.fn(), + setLevel: jest.fn(), + trace: jest.fn(), + warn: jest.fn(), +}; + +export const getLogger = (name: string): log4js$Logger => logger; + +const log4js = { + getLogger, + // $FlowFixMe + configure: jest.fn(), +}; + +export default log4js; diff --git a/jest/__mocks__/nuclide-commons/analytics.js b/jest/__mocks__/nuclide-commons/analytics.js new file mode 100644 index 00000000..cb79ae0d --- /dev/null +++ b/jest/__mocks__/nuclide-commons/analytics.js @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @flow + * @format + */ + +export function trackTiming( + eventName: string, + operation: () => T, + values?: {[key: string]: any} = {}, +): T { + return operation(); +} + +export const track: any = jest.fn(); + +export const startTracking = () => { + const timingTracker: any = { + onError: jest.fn(), + onSuccess: jest.fn(), + }; + + return timingTracker; +}; + +export const trackImmediate: any = jest.fn(); + +export const setRawAnalyticsService: any = jest.fn(); + +export const trackTimingSampled: any = jest.fn((event, fn) => fn()); diff --git a/jest/__tests__/waits_for-test.js b/jest/__tests__/waits_for-test.js new file mode 100644 index 00000000..d19712dc --- /dev/null +++ b/jest/__tests__/waits_for-test.js @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @flow strict + * @format + */ + +import waitsFor from '../waits_for'; + +it('waits', async () => { + let condition = false; + Promise.resolve().then(() => (condition = true)); + await waitsFor(() => condition); +}); + +it("can't wait anymore", async () => { + await expect(waitsFor(() => false, undefined, 1)).rejects.toThrow( + 'but it never did', + ); +}); + +it('gives a message', async () => { + await expect(waitsFor(() => false, 'lol', 1)).rejects.toThrow('lol'); +}); diff --git a/jest/build_atom_jest_runner.sh b/jest/build_atom_jest_runner.sh new file mode 100755 index 00000000..4642c3ef --- /dev/null +++ b/jest/build_atom_jest_runner.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Copyright (c) 2015-present, Facebook, Inc. +# All rights reserved. +# +# This source code is licensed under the license found in the LICENSE file in +# the root directory of this source tree. + +THIS_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +FBSOURCE_ROOT=$(realpath "$THIS_DIR""/../../..") + + +cd "$FBSOURCE_ROOT""/xplat/nuclide/modules/jest-atom-runner" || exit + +./node_modules/.bin/babel src --out-dir build diff --git a/jest/jest.config.atom.js b/jest/jest.config.atom.js new file mode 100644 index 00000000..e7f799fa --- /dev/null +++ b/jest/jest.config.atom.js @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @noflow + */ +'use strict'; + +/* eslint + comma-dangle: [1, always-multiline], + prefer-object-spread/prefer-object-spread: 0, + nuclide-internal/no-commonjs: 0, + */ + +const path = require('path'); + +module.exports = { + displayName: 'atom', + rootDir: path.resolve(__dirname, '../../..'), + roots: ['/xplat/nuclide'], + testMatch: ['**/__atom_tests__/**/*.js?(x)'], + transform: { + '\\.js$': '/xplat/nuclide/modules/nuclide-jest/jestTransformer.js', + }, + setupTestFrameworkScriptFile: '/xplat/nuclide/jest/setupTestFrameworkScriptFile.atom.js', + testPathIgnorePatterns: ['/node_modules/'], + runner: path.resolve(__dirname, '../modules/jest-atom-runner/build/index.js'), + moduleNameMapper: { + oniguruma: path.resolve(__dirname, './__mocks__/emptyObject.js'), + }, + testEnvironment: + '/xplat/nuclide/modules/jest-atom-runner/build/environment.js', +}; diff --git a/jest/jest.config.node.js b/jest/jest.config.node.js new file mode 100644 index 00000000..c446bc7b --- /dev/null +++ b/jest/jest.config.node.js @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @noflow + */ +'use strict'; + +/* eslint + comma-dangle: [1, always-multiline], + prefer-object-spread/prefer-object-spread: 0, + nuclide-internal/no-commonjs: 0, + */ + +const path = require('path'); + +module.exports = { + displayName: 'node', + rootDir: path.resolve(__dirname, '../../../'), + roots: ['/xplat/nuclide'], + testMatch: ['**/__tests__/**/*.js?(x)'], + transform: { + '\\.js$': '/xplat/nuclide/modules/nuclide-jest/jestTransformer.js', + }, + setupTestFrameworkScriptFile: '/xplat/nuclide/jest/setupTestFrameworkScriptFile.node.js', + setupFiles: ['/xplat/nuclide/jest/setup.js'], + testPathIgnorePatterns: ['/node_modules/'], +}; diff --git a/jest/setup.js b/jest/setup.js new file mode 100644 index 00000000..26c0b4af --- /dev/null +++ b/jest/setup.js @@ -0,0 +1,15 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @flow strict + * @format + */ + +jest.mock('nuclide-commons/analytics'); +jest.mock('log4js'); + +global.NUCLIDE_DO_NOT_LOG = true; diff --git a/jest/setupTestFrameworkScriptFile.atom.js b/jest/setupTestFrameworkScriptFile.atom.js new file mode 100644 index 00000000..8d36966b --- /dev/null +++ b/jest/setupTestFrameworkScriptFile.atom.js @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +beforeEach(async () => { + await global.atom.reset(); +}); + +// Disable prompt to download react devtools in atom tests +window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = {isDisabled: true}; diff --git a/jest/setupTestFrameworkScriptFile.node.js b/jest/setupTestFrameworkScriptFile.node.js new file mode 100644 index 00000000..d7f4ed56 --- /dev/null +++ b/jest/setupTestFrameworkScriptFile.node.js @@ -0,0 +1,12 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +jest.setTimeout(10000); diff --git a/jest/waits_for.js b/jest/waits_for.js new file mode 100644 index 00000000..29e82466 --- /dev/null +++ b/jest/waits_for.js @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + * + * @flow strict + * @format + */ + +/* + * Async implementation of Jasmine's waitsFor() + */ +export default (async function waitsFor( + fn: () => boolean, + message?: string, + timeout: number = 4500, +) { + const error = new Error( + message != null + ? message + : 'Expected the function to start returning "true" but it never did', + ); + const startTime = Date.now(); + while (!Boolean(fn())) { + if (Date.now() - startTime > timeout) { + throw error; + } + // eslint-disable-next-line no-await-in-loop + await new Promise(resolve => setTimeout(resolve, 50)); + } +}); diff --git a/scripts/sync-nuclide.sh b/scripts/sync-nuclide.sh index 12cddaae..b79543f6 100755 --- a/scripts/sync-nuclide.sh +++ b/scripts/sync-nuclide.sh @@ -7,6 +7,7 @@ SYNC_PATHS=( 'flow-libs' 'flow-typed' 'modules' + 'jest' ':!modules/big-dig*' ':!modules/nuclide-debugger-cli' ':!modules/nuclide-debugger-vsps'