vitest#vi TypeScript Examples

The following examples show how to use vitest#vi. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: plugins.test.ts    From reskript with MIT License 6 votes vote down vote up
test('one plugin', async () => {
    const settings: ProjectSettings = fillProjectSettings({driver: 'webpack', devServer: {}});
    const plugin = vi.fn((settings: ProjectSettings, cmd: ProjectAware): ProjectSettings => {
        return {
            ...settings,
            devServer: {
                ...settings.devServer,
                port: 8000,
                defaultProxyDomain: cmd.cwd,
            },
        };
    });
    const options = {...BUILD_CMD, cwd: 'cwd'};
    const output = await applyPlugins(settings, [plugin], options);
    expect(plugin).toHaveBeenCalled();
    expect(plugin.mock.calls[0][0]).toBe(settings);
    expect(plugin.mock.calls[0][1]).toBe(options);
    expect(output.devServer.port).toBe(8000);
});
Example #2
Source File: plugins.test.ts    From reskript with MIT License 6 votes vote down vote up
test('plugins factory', async () => {
    const settings: ProjectSettings = fillProjectSettings({driver: 'webpack', devServer: {}});
    const port: SettingsPlugin = settings => {
        return {
            ...settings,
            devServer: {
                ...settings.devServer,
                port: 8000,
            },
        };
    };
    const domain: SettingsPlugin = settings => {
        return {
            ...settings,
            devServer: {
                ...settings.devServer,
                defaultProxyDomain: 'random.api.js',
            },
        };
    };
    const factory = vi.fn((commandName: string) => (commandName === 'build' ? [port, domain] : []));
    const output = await applyPlugins(settings, factory, {cwd: '', commandName: 'build'} as any);
    expect(factory).toHaveBeenCalled();
    expect(factory.mock.calls[0][0]).toBe('build');
    expect(output.devServer.port).toBe(8000);
    expect(output.devServer.defaultProxyDomain).toBe('random.api.js');
});
Example #3
Source File: aws-kms-signer.spec.ts    From cloud-cryptographic-wallet with MIT License 6 votes vote down vote up
function mockSendFunction(
  client: KMSClient,
  getPublicKeyCommandOutput: Partial<GetPublicKeyCommandOutput> | undefined,
  signCommandOutput: Partial<SignCommandOutput> | undefined
) {
  const spy = vi.spyOn(client, "send");

  spy.mockImplementation((command) => {
    if (command instanceof GetPublicKeyCommand && getPublicKeyCommandOutput) {
      return getPublicKeyCommandOutput;
    }
    if (command instanceof SignCommand && signCommandOutput) {
      return signCommandOutput;
    }
  });

  return spy;
}
Example #4
Source File: cloud-kms-signer.spec.ts    From cloud-cryptographic-wallet with MIT License 6 votes vote down vote up
function mockGetPublicKey(
  client: KeyManagementServiceClient,
  publicKeyResponse?: Partial<protos.google.cloud.kms.v1.IPublicKey>
) {
  const pem =
    "-----BEGIN PUBLIC KEY-----\n" +
    "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE1PZkpC+R30dOTpdUn3c6GH7YX5Ovn8QF\n" +
    "OimWHZyBDqM8iJSjE2Mcfu6DkW7zKtXbwyHwax1gDwOe7iJIjWtI0Q==\n" +
    "-----END PUBLIC KEY-----\n";
  const spy = vi.spyOn(client, "getPublicKey");

  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-expect-error
  spy.mockResolvedValue([
    {
      name: "keyName",
      pem,
      pemCrc32c: { value: "4077715619" },
      ...publicKeyResponse,
    },
  ]);

  return spy;
}
Example #5
Source File: cloud-kms-signer.spec.ts    From cloud-cryptographic-wallet with MIT License 6 votes vote down vote up
function mockAsymmetricSign(
  client: KeyManagementServiceClient,
  signatureResponse?: Partial<protos.google.cloud.kms.v1.IAsymmetricSignResponse>
) {
  const spy = vi.spyOn(client, "asymmetricSign");

  const signature = Bytes.fromString(
    "3045022100f66b26e8370aead7cab0fedd9650705be79be165ba807b5e2d1b18030d726d2302204e77b43a05c5901c38ed60957bc38dadea4a6b91837815f197a520871fa53e72"
  );

  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-expect-error
  spy.mockResolvedValue([
    {
      signature: signature.asUint8Array,
      signatureCrc32c: { value: "3151495169" },
      ...signatureResponse,
    },
  ]);

  return spy;
}
Example #6
Source File: finalize.test.ts    From reskript with MIT License 5 votes vote down vote up
describe('finalize', () => {
    test('can receive a fully resolved webpack config and modify it', async () => {
        const finalize = vi.fn((config: FinalizableWebpackConfiguration) => ({...config, mode: 'production' as const}));
        const options = {...BUILD_CMD, cwd: currentDirectory};
        const projectSettings = await readProjectSettings(options) as WebpackProjectSettings;
        const withFinalize = {
            ...projectSettings,
            build: {
                ...projectSettings.build,
                finalize,
            },
        };
        const context: BuildContext = {
            cwd: currentDirectory,
            mode: 'development',
            usage: 'build',
            srcDirectory: 'src',
            hostPackageName: 'test',
            buildVersion: '000000',
            buildTime: (new Date()).toISOString(),
            features: {},
            buildTarget: 'stable',
            isDefaultTarget: false,
            entries: [],
            projectSettings: withFinalize,
        };
        const config = await createWebpackConfig(context);
        expect(finalize).toHaveBeenCalled();
        expect(typeof finalize.mock.calls[0][0]).toBe('object');
        expect(typeof finalize.mock.calls[0][0].module).toBe('object');
        expect(config.mode).toBe('production');
    });

    test('can modify babel config', async () => {
        const finalize = vi.fn((config: TransformOptions) => ({...config, comments: false}));
        const options = {...BUILD_CMD, cwd: currentDirectory};
        const projectSettings = await readProjectSettings(options) as WebpackProjectSettings;
        const withFinalize = {
            ...projectSettings,
            build: {
                ...projectSettings.build,
                script: {
                    ...projectSettings.build.script,
                    finalize,
                },
            },
        };
        const context: BuildContext = {
            cwd: currentDirectory,
            mode: 'development',
            usage: 'build',
            srcDirectory: 'src',
            hostPackageName: 'test',
            buildVersion: '000000',
            buildTime: (new Date()).toISOString(),
            features: {},
            buildTarget: 'stable',
            isDefaultTarget: false,
            entries: [],
            projectSettings: withFinalize,
        };
        await createWebpackConfig(context);
        expect(finalize).toHaveBeenCalled();
        expect(typeof finalize.mock.calls[0][0]).toBe('object');
        expect(typeof finalize.mock.calls[0][0].presets).toBe('object');
    });
});
Example #7
Source File: processDatabase.test.ts    From kanel with MIT License 5 votes vote down vote up
vi.mock('extract-pg-schema', () => ({
  extractSchema: () => dvdRental,
}));
Example #8
Source File: processDatabase.test.ts    From kanel with MIT License 5 votes vote down vote up
vi.mock('./writeFile');
Example #9
Source File: processDatabase.test.ts    From kanel with MIT License 5 votes vote down vote up
vi.mock('fs');
Example #10
Source File: processDatabase.test.ts    From kanel with MIT License 5 votes vote down vote up
vi.mock('path', async () => {
  const originalModule = (await vi.importActual('path')) as any;
  return {
    __esModule: false,
    ...originalModule,
    resolve: (...args) => args.join('/'),
  };
});
Example #11
Source File: vitest.setup.ts    From v-perfect-signature with MIT License 5 votes vote down vote up
// @ts-expect-error: type
global.jest = vi