1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16- import { beforeEach , afterEach , describe , it , vi , expect , afterAll } from 'vitest' ;
16+ import { beforeEach , afterEach , describe , it , vi , expect , afterAll , MockInstance } from 'vitest' ;
1717
1818import AudienceEvaluator , { createAudienceEvaluator } from './index' ;
1919import * as conditionTreeEvaluator from '../condition_tree_evaluator' ;
@@ -23,6 +23,9 @@ import { getMockLogger } from '../../tests/mock/mock_logger';
2323import { Audience , OptimizelyDecideOption , OptimizelyDecision } from '../../shared_types' ;
2424import { IOptimizelyUserContext } from '../../optimizely_user_context' ;
2525
26+ vi . mock ( '../condition_tree_evaluator' , { spy : true } ) ;
27+ vi . mock ( '../custom_attribute_condition_evaluator' , { spy : true } ) ;
28+
2629let mockLogger = getMockLogger ( ) ;
2730
2831const getMockUserContext = ( attributes ?: unknown , segments ?: string [ ] ) : IOptimizelyUserContext => ( {
@@ -207,6 +210,9 @@ describe('lib/core/audience_evaluator', () => {
207210 } ) ;
208211
209212 describe ( 'integration with dependencies' , ( ) => {
213+ const evaluateSpy = conditionTreeEvaluator . evaluate as unknown as MockInstance < typeof conditionTreeEvaluator . evaluate > ;
214+ const getEvaluatorSpy = customAttributeConditionEvaluator . getEvaluator as unknown as MockInstance < typeof customAttributeConditionEvaluator . getEvaluator > ;
215+
210216 beforeEach ( ( ) => {
211217 vi . clearAllMocks ( ) ;
212218 } ) ;
@@ -220,7 +226,7 @@ describe('lib/core/audience_evaluator', () => {
220226 } ) ;
221227
222228 it ( 'returns true if conditionTreeEvaluator.evaluate returns true' , ( ) => {
223- vi . spyOn ( conditionTreeEvaluator , 'evaluate' ) . mockReturnValue ( true ) ;
229+ evaluateSpy . mockReturnValue ( true ) ;
224230 const result = audienceEvaluator . evaluate (
225231 [ 'or' , '0' , '1' ] ,
226232 audiencesById ,
@@ -230,7 +236,7 @@ describe('lib/core/audience_evaluator', () => {
230236 } ) ;
231237
232238 it ( 'returns false if conditionTreeEvaluator.evaluate returns false' , ( ) => {
233- vi . spyOn ( conditionTreeEvaluator , 'evaluate' ) . mockReturnValue ( false ) ;
239+ evaluateSpy . mockReturnValue ( false ) ;
234240 const result = audienceEvaluator . evaluate (
235241 [ 'or' , '0' , '1' ] ,
236242 audiencesById ,
@@ -240,7 +246,7 @@ describe('lib/core/audience_evaluator', () => {
240246 } ) ;
241247
242248 it ( 'returns false if conditionTreeEvaluator.evaluate returns null' , ( ) => {
243- vi . spyOn ( conditionTreeEvaluator , 'evaluate' ) . mockReturnValue ( null ) ;
249+ evaluateSpy . mockReturnValue ( null ) ;
244250 const result = audienceEvaluator . evaluate (
245251 [ 'or' , '0' , '1' ] ,
246252 audiencesById ,
@@ -250,13 +256,13 @@ describe('lib/core/audience_evaluator', () => {
250256 } ) ;
251257
252258 it ( 'calls customAttributeConditionEvaluator.evaluate in the leaf evaluator for audience conditions' , ( ) => {
253- vi . spyOn ( conditionTreeEvaluator , 'evaluate' ) . mockImplementation ( ( conditions : any , leafEvaluator ) => {
259+ evaluateSpy . mockImplementation ( ( conditions : any , leafEvaluator : any ) => {
254260 return leafEvaluator ( conditions [ 1 ] ) ;
255261 } ) ;
256262
257263 const mockCustomAttributeConditionEvaluator = vi . fn ( ) . mockReturnValue ( false ) ;
258264
259- vi . spyOn ( customAttributeConditionEvaluator , 'getEvaluator' ) . mockReturnValue ( {
265+ getEvaluatorSpy . mockReturnValue ( {
260266 evaluate : mockCustomAttributeConditionEvaluator ,
261267 } ) ;
262268
@@ -278,11 +284,15 @@ describe('lib/core/audience_evaluator', () => {
278284
279285 describe ( 'Audience evaluation logging' , ( ) => {
280286 let mockCustomAttributeConditionEvaluator : ReturnType < typeof vi . fn > ;
281-
287+
288+ const evaluateSpy = conditionTreeEvaluator . evaluate as unknown as MockInstance < typeof conditionTreeEvaluator . evaluate > ;
289+ const getEvaluatorSpy = customAttributeConditionEvaluator . getEvaluator as unknown as MockInstance < typeof customAttributeConditionEvaluator . getEvaluator > ;
290+
282291 beforeEach ( ( ) => {
292+ vi . clearAllMocks ( ) ;
293+
283294 mockCustomAttributeConditionEvaluator = vi . fn ( ) ;
284- vi . spyOn ( conditionTreeEvaluator , 'evaluate' ) ;
285- vi . spyOn ( customAttributeConditionEvaluator , 'getEvaluator' ) . mockReturnValue ( {
295+ getEvaluatorSpy . mockReturnValue ( {
286296 evaluate : mockCustomAttributeConditionEvaluator ,
287297 } ) ;
288298 } ) ;
@@ -292,7 +302,7 @@ describe('lib/core/audience_evaluator', () => {
292302 } ) ;
293303
294304 it ( 'logs correctly when conditionTreeEvaluator.evaluate returns null' , ( ) => {
295- vi . spyOn ( conditionTreeEvaluator , 'evaluate' ) . mockImplementationOnce ( ( conditions : any , leafEvaluator ) => {
305+ evaluateSpy . mockImplementationOnce ( ( conditions : any , leafEvaluator ) => {
296306 return leafEvaluator ( conditions [ 1 ] ) ;
297307 } ) ;
298308
@@ -319,7 +329,7 @@ describe('lib/core/audience_evaluator', () => {
319329 } ) ;
320330
321331 it ( 'logs correctly when conditionTreeEvaluator.evaluate returns true' , ( ) => {
322- vi . spyOn ( conditionTreeEvaluator , 'evaluate' ) . mockImplementationOnce ( ( conditions : any , leafEvaluator ) => {
332+ evaluateSpy . mockImplementationOnce ( ( conditions : any , leafEvaluator ) => {
323333 return leafEvaluator ( conditions [ 1 ] ) ;
324334 } ) ;
325335
@@ -345,7 +355,7 @@ describe('lib/core/audience_evaluator', () => {
345355 } ) ;
346356
347357 it ( 'logs correctly when conditionTreeEvaluator.evaluate returns false' , ( ) => {
348- vi . spyOn ( conditionTreeEvaluator , 'evaluate' ) . mockImplementationOnce ( ( conditions : any , leafEvaluator ) => {
358+ evaluateSpy . mockImplementationOnce ( ( conditions : any , leafEvaluator ) => {
349359 return leafEvaluator ( conditions [ 1 ] ) ;
350360 } ) ;
351361
0 commit comments