Skip to content

Commit e70c6a7

Browse files
committed
add lib mocking back
1 parent d5729bb commit e70c6a7

File tree

1 file changed

+69
-13
lines changed

1 file changed

+69
-13
lines changed

src/validation/validation.test.ts

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import { readFileSync } from 'node:fs';
88
import { join } from 'node:path';
9+
import { vi } from 'vitest';
910

1011
import { AjvJsonSchemaValidator } from './ajv-provider.js';
1112
import { CfWorkerJsonSchemaValidator } from './cfworker-provider.js';
@@ -531,23 +532,41 @@ describe('JSON Schema Validators', () => {
531532
});
532533

533534
describe('Missing dependencies', () => {
534-
describe('Module independence', () => {
535-
it('should be able to import ajv-provider when @cfworker/json-schema is missing', async () => {
536-
// ajv-provider doesn't depend on @cfworker/json-schema, so it should import fine
537-
// even if we can't import cfworker-provider
538-
const ajvModule = await import('./ajv-provider.js');
539-
expect(ajvModule.AjvJsonSchemaValidator).toBeDefined();
535+
describe('AJV not installed but CfWorker is', () => {
536+
beforeEach(() => {
537+
vi.resetModules();
538+
});
540539

541-
// And should work correctly
542-
const validator = new ajvModule.AjvJsonSchemaValidator();
543-
const schema: JsonSchemaType = { type: 'string' };
544-
const validatorFn = validator.getValidator(schema);
545-
expect(validatorFn('test').valid).toBe(true);
540+
afterEach(() => {
541+
vi.doUnmock('ajv');
542+
vi.doUnmock('ajv-formats');
543+
});
544+
545+
it('should throw error when trying to import ajv-provider without ajv', async () => {
546+
// Mock ajv as not installed
547+
vi.doMock('ajv', () => {
548+
throw new Error("Cannot find module 'ajv'");
549+
});
550+
551+
vi.doMock('ajv-formats', () => {
552+
throw new Error("Cannot find module 'ajv-formats'");
553+
});
554+
555+
// Attempting to import ajv-provider should fail
556+
await expect(import('./ajv-provider.js')).rejects.toThrow();
546557
});
547558

548559
it('should be able to import cfworker-provider when ajv is missing', async () => {
549-
// cfworker-provider doesn't depend on ajv, so it should import fine
550-
// even if we can't import ajv-provider
560+
// Mock ajv as not installed
561+
vi.doMock('ajv', () => {
562+
throw new Error("Cannot find module 'ajv'");
563+
});
564+
565+
vi.doMock('ajv-formats', () => {
566+
throw new Error("Cannot find module 'ajv-formats'");
567+
});
568+
569+
// But cfworker-provider should import successfully
551570
const cfworkerModule = await import('./cfworker-provider.js');
552571
expect(cfworkerModule.CfWorkerJsonSchemaValidator).toBeDefined();
553572

@@ -557,6 +576,43 @@ describe('Missing dependencies', () => {
557576
const validatorFn = validator.getValidator(schema);
558577
expect(validatorFn('test').valid).toBe(true);
559578
});
579+
});
580+
581+
describe('CfWorker not installed but AJV is', () => {
582+
beforeEach(() => {
583+
vi.resetModules();
584+
});
585+
586+
afterEach(() => {
587+
vi.doUnmock('@cfworker/json-schema');
588+
});
589+
590+
it('should throw error when trying to import cfworker-provider without @cfworker/json-schema', async () => {
591+
// Mock @cfworker/json-schema as not installed
592+
vi.doMock('@cfworker/json-schema', () => {
593+
throw new Error("Cannot find module '@cfworker/json-schema'");
594+
});
595+
596+
// Attempting to import cfworker-provider should fail
597+
await expect(import('./cfworker-provider.js')).rejects.toThrow();
598+
});
599+
600+
it('should be able to import ajv-provider when @cfworker/json-schema is missing', async () => {
601+
// Mock @cfworker/json-schema as not installed
602+
vi.doMock('@cfworker/json-schema', () => {
603+
throw new Error("Cannot find module '@cfworker/json-schema'");
604+
});
605+
606+
// But ajv-provider should import successfully
607+
const ajvModule = await import('./ajv-provider.js');
608+
expect(ajvModule.AjvJsonSchemaValidator).toBeDefined();
609+
610+
// And should work correctly
611+
const validator = new ajvModule.AjvJsonSchemaValidator();
612+
const schema: JsonSchemaType = { type: 'string' };
613+
const validatorFn = validator.getValidator(schema);
614+
expect(validatorFn('test').valid).toBe(true);
615+
});
560616

561617
it('should document that @cfworker/json-schema is required', () => {
562618
const cfworkerProviderPath = join(__dirname, 'cfworker-provider.ts');

0 commit comments

Comments
 (0)