@@ -6,34 +6,35 @@ import type { PluginOptions } from '@dd/core/types';
66import {
77 getGetPluginsArg ,
88 getContextMock ,
9- getMockTimer ,
10- getMockLogger ,
9+ getMockData ,
10+ getMockStores ,
1111} from '@dd/tests/_jest/helpers/mocks' ;
1212
13+ import { getLoggerFactory } from './logger' ;
1314import { wrapGetPlugins , wrapHook , wrapPlugin } from './wrapPlugins' ;
1415
1516const wait = ( duration : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , duration ) ) ;
1617describe ( 'profilePlugins' , ( ) => {
1718 describe ( 'wrapGetPlugins' , ( ) => {
18- const mockTimer = getMockTimer ( ) ;
19- const mockLogger = getMockLogger ( {
20- time : jest . fn ( ( ) => mockTimer ) ,
21- } ) ;
22- const mockContext = getContextMock ( {
23- getLogger : jest . fn ( ( ) => mockLogger ) ,
24- } ) ;
25- const mockGetPlugins = jest . fn ( ( ) => {
26- return [
27- {
28- name : 'datadog-test-1-plugin' ,
29- } ,
30- {
31- name : 'datadog-test-2-plugin' ,
32- } ,
33- ] ;
34- } ) ;
19+ const logger = getLoggerFactory ( getMockData ( ) , getMockStores ( ) ) ( 'fake-logger' ) ;
20+ const timer = logger . time ( 'fake-timer' ) ;
21+ jest . spyOn ( timer , 'end' ) ;
22+ jest . spyOn ( logger , 'time' ) . mockReturnValue ( timer ) ;
3523
3624 test ( 'Should wrap the getPlugins function and measure initialization time' , ( ) => {
25+ const mockContext = getContextMock ( ) ;
26+ const mockGetPlugins = jest . fn ( ( ) => {
27+ return [
28+ {
29+ name : 'datadog-test-1-plugin' ,
30+ } ,
31+ {
32+ name : 'datadog-test-2-plugin' ,
33+ } ,
34+ ] ;
35+ } ) ;
36+ jest . spyOn ( mockContext , 'getLogger' ) . mockReturnValue ( logger ) ;
37+
3738 const wrappedGetPlugins = wrapGetPlugins ( mockContext , mockGetPlugins , 'test-plugins' ) ;
3839 const pluginsArg = getGetPluginsArg ( ) ;
3940 const result = wrappedGetPlugins ( pluginsArg ) ;
@@ -42,15 +43,15 @@ describe('profilePlugins', () => {
4243 expect ( mockGetPlugins ) . toHaveBeenCalledWith ( pluginsArg ) ;
4344
4445 // Verify timer was started and ended
45- expect ( mockLogger . time ) . toHaveBeenCalledWith (
46+ expect ( logger . time ) . toHaveBeenCalledWith (
4647 'hook | init test-plugins' ,
4748 expect . any ( Object ) ,
4849 ) ;
49- expect ( mockTimer . end ) . toHaveBeenCalledTimes ( 1 ) ;
50+ expect ( timer . end ) . toHaveBeenCalledTimes ( 1 ) ;
5051
5152 // Verify the timer got tagged with the plugin names.
52- expect ( mockTimer . timer . tags ) . toContain ( 'plugin:datadog-test-1-plugin' ) ;
53- expect ( mockTimer . timer . tags ) . toContain ( 'plugin:datadog-test-2-plugin' ) ;
53+ expect ( timer . timer . tags ) . toContain ( 'plugin:datadog-test-1-plugin' ) ;
54+ expect ( timer . timer . tags ) . toContain ( 'plugin:datadog-test-2-plugin' ) ;
5455
5556 // Verify the result contains the expected plugins
5657 expect ( result ) . toHaveLength ( 2 ) ;
@@ -60,26 +61,15 @@ describe('profilePlugins', () => {
6061 } ) ;
6162
6263 describe ( 'wrapPlugin' , ( ) => {
63- const mockTimer = getMockTimer ( ) ;
64- const mockLogger = getMockLogger ( {
65- time : jest . fn ( ( ) => mockTimer ) ,
66- } ) ;
64+ const logger = getLoggerFactory ( getMockData ( ) , getMockStores ( ) ) ( 'fake-logger' ) ;
6765 const mockPlugin : PluginOptions = {
6866 name : 'datadog-test-1-plugin' ,
6967 buildStart : jest . fn ( ) ,
7068 resolveId : jest . fn ( ) ,
7169 } ;
7270
73- beforeAll ( ( ) => {
74- jest . useFakeTimers ( ) ;
75- } ) ;
76-
77- afterAll ( ( ) => {
78- jest . useRealTimers ( ) ;
79- } ) ;
80-
8171 test ( "Should wrap the plugin's hooks." , async ( ) => {
82- const plugin = wrapPlugin ( mockPlugin , mockLogger ) ;
72+ const plugin = wrapPlugin ( mockPlugin , logger ) ;
8373
8474 // Verify hooks are wrapped.
8575 expect ( plugin . buildStart ) . toBeDefined ( ) ;
@@ -90,10 +80,10 @@ describe('profilePlugins', () => {
9080 } ) ;
9181
9282 describe ( 'wrapHook' , ( ) => {
93- const mockTimer = getMockTimer ( ) ;
94- const mockLogger = getMockLogger ( {
95- time : jest . fn ( ( ) => mockTimer ) ,
96- } ) ;
83+ const logger = getLoggerFactory ( getMockData ( ) , getMockStores ( ) ) ( 'fake-logger' ) ;
84+ const timer = logger . time ( 'fake-timer' ) ;
85+ jest . spyOn ( timer , 'end' ) ;
86+ jest . spyOn ( logger , 'time' ) . mockReturnValue ( timer ) ;
9787
9888 const mockPlugin : PluginOptions = {
9989 name : 'datadog-test-1-plugin' ,
@@ -120,15 +110,15 @@ describe('profilePlugins', () => {
120110 mockPlugin . name ,
121111 'buildStart' ,
122112 mockPlugin . transform ! ,
123- mockLogger ,
113+ logger ,
124114 ) ;
125115
126116 const prom = wrappedHook ( ) ;
127117 jest . advanceTimersByTime ( 500 ) ;
128118 const result = await prom ;
129119
130- expect ( mockTimer . end ) . toHaveBeenCalledTimes ( 1 ) ;
131- expect ( mockTimer . timer . total ) . toBeGreaterThan ( 500 ) ;
120+ expect ( timer . end ) . toHaveBeenCalledTimes ( 1 ) ;
121+ expect ( timer . timer . total ) . toBeGreaterThan ( 500 ) ;
132122 expect ( result ) . toBe ( 'transform' ) ;
133123 } ) ;
134124
@@ -137,15 +127,15 @@ describe('profilePlugins', () => {
137127 mockPlugin . name ,
138128 'buildStart' ,
139129 mockPlugin . resolveId ! ,
140- mockLogger ,
130+ logger ,
141131 ) ;
142132
143133 const prom = wrappedHook ( ) ;
144134 jest . advanceTimersByTime ( 500 ) ;
145135
146136 await expect ( prom ) . rejects . toThrow ( 'resolveId' ) ;
147- expect ( mockTimer . end ) . toHaveBeenCalledTimes ( 1 ) ;
148- expect ( mockTimer . timer . total ) . toBeGreaterThan ( 500 ) ;
137+ expect ( timer . end ) . toHaveBeenCalledTimes ( 1 ) ;
138+ expect ( timer . timer . total ) . toBeGreaterThan ( 500 ) ;
149139 } ) ;
150140 } ) ;
151141} ) ;
0 commit comments