Skip to content

Commit bedd68f

Browse files
authored
Vitest (#92)
# Description Migrates from Jest to Vitest ## Issue Ticket Number <!-- Specifiy which issue this fixes by referencing the issue number (`#11`) or issue URL. --> <!-- Example: Fixes #1 --> Fixes #90 ## Type of change <!-- Please select all options that are applicable. --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update # Checklist <!-- These must all be followed and checked. --> - [ ] I have followed the contributing guidelines of this project as mentioned in [CONTRIBUTING.md](/CONTRIBUTING.md) - [ ] I have created an [issue](https://github.com/colbyfayock/netlify-plugin-cloudinary/issues) ticket for this PR - [ ] I have checked to ensure there aren't other open [Pull Requests](https://github.com/colbyfayock/netlify-plugin-cloudinary/pulls) for the same update/change? - [ ] I have performed a self-review of my own code - [ ] I have run tests locally to ensure they all pass - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes needed to the documentation
1 parent f4676de commit bedd68f

File tree

11 files changed

+830
-1790
lines changed

11 files changed

+830
-1790
lines changed

netlify-plugin-cloudinary/.npmignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
.prettierignore
55
.prettierrc
66
.setTestEnvVars.js
7-
jest.config.js
87
node_modules
98
src
109
tsconfig.json

netlify-plugin-cloudinary/.setTestEnvVars.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

netlify-plugin-cloudinary/jest.config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

netlify-plugin-cloudinary/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"format": "prettier --write \"(src|test)/*.+(js|ts|json)\"",
1010
"postpublish": "rm ./README.md",
1111
"prepublishOnly": "cp ../README.md . && pnpm build",
12-
"test": "jest",
12+
"test": "vitest run",
1313
"typecheck": "tsc --noEmit -p ./tsconfig.json"
1414
},
1515
"author": {
@@ -38,9 +38,11 @@
3838
"eslint-plugin-import": "2.28.1",
3939
"eslint-plugin-n": "16.0.2",
4040
"eslint-plugin-promise": "6.1.1",
41-
"jest": "29.6.4",
4241
"prettier": "3.0.3",
43-
"ts-jest": "29.1.1",
44-
"typescript": "5.2.2"
42+
"typescript": "5.2.2",
43+
"vitest": "^0.34.6"
44+
},
45+
"engines": {
46+
"node": ">=16"
4547
}
4648
}

netlify-plugin-cloudinary/tests/lib/cloudinary-cname.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
const { configureCloudinary, getCloudinaryUrl, updateHtmlImagesToCloudinary } = require('../../src/lib/cloudinary');
1+
import { vi, expect, describe, test, beforeEach, afterAll, it } from 'vitest';
2+
3+
import { configureCloudinary, getCloudinaryUrl, updateHtmlImagesToCloudinary } from '../../src/lib/cloudinary';
24

35
describe('lib/util', () => {
46
const ENV_ORIGINAL = process.env;
57
const cname = 'spacejelly.dev';
68

79
beforeEach(() => {
8-
jest.resetModules();
10+
vi.resetModules();
911

1012
process.env = { ...ENV_ORIGINAL };
1113
process.env.CLOUDINARY_CLOUD_NAME = 'testcloud';

netlify-plugin-cloudinary/tests/lib/cloudinary-privatecdn.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
const { configureCloudinary, getCloudinaryUrl, updateHtmlImagesToCloudinary } = require('../../src/lib/cloudinary');
1+
import { vi, expect, describe, test, beforeEach, afterAll, it } from 'vitest';
2+
3+
import { configureCloudinary, getCloudinaryUrl, updateHtmlImagesToCloudinary } from '../../src/lib/cloudinary';
24

35
describe('lib/util', () => {
46
const ENV_ORIGINAL = process.env;
57

68
beforeEach(() => {
7-
jest.resetModules();
9+
vi.resetModules();
810

911
process.env = { ...ENV_ORIGINAL };
1012
process.env.CLOUDINARY_CLOUD_NAME = 'testcloud';

netlify-plugin-cloudinary/tests/lib/cloudinary.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
const { ERROR_ASSET_UPLOAD, ERROR_INVALID_SRCSET } = require('../../src/data/errors');
2-
const { getCloudinary, createPublicId, getCloudinaryUrl, configureCloudinary, updateHtmlImagesToCloudinary } = require('../../src/lib/cloudinary');
1+
import { vi, expect, describe, test, beforeEach, afterAll, it } from 'vitest';
2+
3+
import { ERROR_ASSET_UPLOAD } from '../../src/data/errors';
4+
import { getCloudinary, createPublicId, configureCloudinary, getCloudinaryUrl, updateHtmlImagesToCloudinary } from '../../src/lib/cloudinary';
35

46
const mockDemo = require('../mocks/demo.json');
57

@@ -9,7 +11,7 @@ describe('lib/util', () => {
911
const ENV_ORIGINAL = process.env;
1012

1113
beforeEach(() => {
12-
jest.resetModules();
14+
vi.resetModules();
1315

1416
process.env = { ...ENV_ORIGINAL };
1517
process.env.CLOUDINARY_CLOUD_NAME = 'testcloud';
@@ -73,7 +75,7 @@ describe('lib/util', () => {
7375

7476
test('should create a Cloudinary URL with delivery type of upload from a local image', async () => {
7577
// mock cloudinary.uploader.upload call
76-
cloudinary.uploader.upload = jest.fn().mockResolvedValue({
78+
cloudinary.uploader.upload = vi.fn().mockResolvedValue({
7779
public_id: 'stranger-things-dustin-fc571e771d5ca7d9223a7eebfd2c505d',
7880
secure_url: `https://res.cloudinary.com/${process.env.CLOUDINARY_CLOUD_NAME}/image/upload/v1613009008/stranger-things-dustin-fc571e771d5ca7d9223a7eebfd2c505d.jpg`,
7981
original_filename: 'stranger-things-dustin',
@@ -97,7 +99,7 @@ describe('lib/util', () => {
9799

98100
test('should fail to create a Cloudinary URL with delivery type of upload', async () => {
99101
// mock cloudinary.uploader.upload call
100-
cloudinary.uploader.upload = jest.fn().mockImplementation(() => Promise.reject('error'))
102+
cloudinary.uploader.upload = vi.fn().mockImplementation(() => Promise.reject('error'))
101103

102104

103105
await expect(getCloudinaryUrl({

netlify-plugin-cloudinary/tests/lib/util.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const { isRemoteUrl, determineRemoteUrl, getQueryParams } = require('../../src/lib/util');
1+
import { expect, describe, test } from 'vitest';
2+
3+
import { isRemoteUrl, determineRemoteUrl, getQueryParams } from '../../src/lib/util';
24

35
describe('lib/util', () => {
46

netlify-plugin-cloudinary/tests/on-build.test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
const fs = require('fs').promises;
2-
const { onBuild } = require('../src/');
3-
const { ERROR_API_CREDENTIALS_REQUIRED } = require('../src/data/errors');
1+
import { vi, expect, describe, test, beforeEach,afterEach } from 'vitest';
2+
import { promises as fs } from 'fs';
3+
import { onBuild } from '../src/';
4+
import { ERROR_API_CREDENTIALS_REQUIRED } from '../src/data/errors';
45

56
describe('onBuild', () => {
67
const ENV_ORIGINAL = process.env;
78
const readdir = fs.readdir;
89

910
beforeEach(() => {
10-
fs.readdir = jest.fn();
11-
jest.resetModules();
11+
fs.readdir = vi.fn();
12+
vi.resetModules();
1213

1314
process.env = { ...ENV_ORIGINAL };
1415

@@ -31,7 +32,7 @@ describe('onBuild', () => {
3132
// We don't need a "special" test for this as it's default functionality that should work with
3233
// any valid test, so we can isntead ensure the keys don't exist and delete them
3334

34-
jest.spyOn(global.console, 'error').mockImplementation();
35+
vi.spyOn(global.console, 'error').mockImplementation();
3536

3637
delete process.env.CLOUDINARY_API_KEY;
3738
delete process.env.CLOUDINARY_API_SECRET;

netlify-plugin-cloudinary/tests/on-post-build.test.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
const fs = require('fs').promises;
2-
const path = require('path');
3-
const { JSDOM } = require('jsdom');
4-
5-
const { onPostBuild } = require('../src/');
1+
import { vi, expect, describe, test, beforeAll, afterAll, beforeEach, afterEach } from 'vitest';
2+
import { promises as fs } from 'fs';
3+
import path from 'path';
4+
import { JSDOM } from 'jsdom';
5+
import { onPostBuild } from '../src/';
66

77
const mocksPath = path.join(__dirname, 'mocks/html');
88
const tempPath = path.join(mocksPath, 'temp');
99

1010
async function mkdir(directoryPath) {
1111
let dir;
12+
console.log('directoryPath', directoryPath)
1213
try {
1314
dir = await fs.stat(directoryPath);
1415
} catch(e) {}
1516
if ( dir && dir.isDirectory() ) return;
17+
console.log('mkdir')
1618
await fs.mkdir(directoryPath);
1719
}
1820

@@ -24,7 +26,7 @@ describe('onPostBuild', () => {
2426
});
2527

2628
beforeEach(async () => {
27-
jest.resetModules();
29+
vi.resetModules();
2830

2931
process.env = { ...ENV_ORIGINAL };
3032

@@ -34,7 +36,7 @@ describe('onPostBuild', () => {
3436
process.env.CLOUDINARY_API_SECRET = 'abcd1234';
3537

3638
const mockFiles = (await fs.readdir(mocksPath)).filter(filePath => filePath.includes('.html'));
37-
const tempTestPath = path.join(tempPath, expect.getState().currentTestName);
39+
const tempTestPath = path.join(tempPath, expect.getState().currentTestName.replace('/', '_'));
3840
await mkdir(tempTestPath);
3941
await Promise.all(mockFiles.map(async file => {
4042
await fs.copyFile(path.join(mocksPath, file), path.join(tempTestPath, file));
@@ -44,7 +46,7 @@ describe('onPostBuild', () => {
4446
afterEach(async () => {
4547
process.env = ENV_ORIGINAL;
4648

47-
await fs.rm(path.join(tempPath, expect.getState().currentTestName), { recursive: true, force: true });
49+
await fs.rm(path.join(tempPath, expect.getState().currentTestName.replace('/', '_')), { recursive: true, force: true });
4850
});
4951

5052
afterAll(async () => {
@@ -64,7 +66,7 @@ describe('onPostBuild', () => {
6466

6567
const deliveryType = 'fetch';
6668

67-
const tempTestPath = path.join(tempPath, expect.getState().currentTestName);
69+
const tempTestPath = path.join(tempPath, expect.getState().currentTestName.replace('/', '_'));
6870

6971
await onPostBuild({
7072
constants: {
@@ -103,7 +105,7 @@ describe('onPostBuild', () => {
103105

104106
const deliveryType = 'fetch';
105107

106-
const tempTestPath = path.join(tempPath, expect.getState().currentTestName);
108+
const tempTestPath = path.join(tempPath, expect.getState().currentTestName.replace('/', '_'));
107109

108110
const maxSize = {
109111
width: 800,

0 commit comments

Comments
 (0)