33 *
44 * This source code is licensed under the MIT license found in the
55 * LICENSE file in the root directory of this source tree.
6- *
7- * @flow
86 */
97
10- import type { Script } from 'vm' ;
11- import type { ProjectConfig } from 'types/Config' ;
12- import type { Global } from 'types/Global' ;
13- import type { ModuleMocker } from 'jest-mock' ;
14-
15- import vm from 'vm' ;
16- import { JestFakeTimers as FakeTimers } from '@jest/fake-timers' ;
8+ import vm , { Script , Context } from 'vm' ;
9+ import { Global , Config } from '@jest/types' ;
10+ import { ModuleMocker } from 'jest-mock' ;
1711import { installCommonGlobals } from 'jest-util' ;
18- import mock from 'jest-mock' ;
12+ import { JestFakeTimers as FakeTimers } from '@jest/fake-timers' ;
13+ import { JestEnvironment } from '@jest/environment' ;
1914
20- type Timer = { |
21- id : number ,
22- ref : ( ) => Timer ,
23- unref : ( ) => Timer ,
24- | } ;
15+ type Timer = {
16+ id : number ;
17+ ref : ( ) => Timer ;
18+ unref : ( ) => Timer ;
19+ } ;
2520
26- class NodeEnvironment {
27- context : ? vm$ Context;
28- fakeTimers : ? FakeTimers < Timer > ;
29- global : ? Global ;
30- moduleMocker : ? ModuleMocker ;
21+ class NodeEnvironment implements JestEnvironment {
22+ context : Context | null ;
23+ fakeTimers : FakeTimers < Timer > | null ;
24+ global : Global . Global ;
25+ moduleMocker : ModuleMocker | null ;
3126
32- constructor ( config : ProjectConfig ) {
27+ constructor ( config : Config . ProjectConfig ) {
3328 this . context = vm . createContext ( ) ;
3429 const global = ( this . global = vm . runInContext (
3530 'this' ,
@@ -48,7 +43,7 @@ class NodeEnvironment {
4843 global . URLSearchParams = URLSearchParams ;
4944 }
5045 installCommonGlobals ( global , config . globals ) ;
51- this . moduleMocker = new mock . ModuleMocker ( global ) ;
46+ this . moduleMocker = new ModuleMocker ( global ) ;
5247
5348 const timerIdToRef = ( id : number ) => ( {
5449 id,
@@ -60,7 +55,8 @@ class NodeEnvironment {
6055 } ,
6156 } ) ;
6257
63- const timerRefToId = ( timer : Timer ) : ?number => ( timer && timer . id ) || null ;
58+ const timerRefToId = ( timer : Timer ) : number | undefined =>
59+ ( timer && timer . id ) || undefined ;
6460
6561 const timerConfig = {
6662 idToRef : timerIdToRef ,
@@ -75,11 +71,11 @@ class NodeEnvironment {
7571 } ) ;
7672 }
7773
78- setup ( ) : Promise < void > {
74+ setup ( ) {
7975 return Promise . resolve ( ) ;
8076 }
8177
82- teardown ( ) : Promise < void > {
78+ teardown ( ) {
8379 if ( this . fakeTimers ) {
8480 this . fakeTimers . dispose ( ) ;
8581 }
@@ -88,13 +84,14 @@ class NodeEnvironment {
8884 return Promise . resolve ( ) ;
8985 }
9086
91- // Disabling rule as return type depends on script's return type.
92- runScript ( script : Script ) : ?any {
87+ // TS infers the return type to be `any`, since that's what `runInContext`
88+ // returns.
89+ runScript ( script : Script ) {
9390 if ( this . context ) {
9491 return script . runInContext ( this . context ) ;
9592 }
9693 return null ;
9794 }
9895}
9996
100- module . exports = NodeEnvironment ;
97+ export = NodeEnvironment ;
0 commit comments