5
0
Fork 0
mirror of https://github.com/hashicorp/vault-action.git synced 2025-11-07 15:16:56 +00:00

wip: replace jest with vitest

This commit is contained in:
JM Faircloth 2024-03-08 16:57:50 -06:00
parent b379e88781
commit fcac78057e
22 changed files with 2182 additions and 78 deletions

3
dist/package.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"type": "module"
}

View file

@ -1,5 +1,7 @@
jest.mock('@actions/core');
jest.mock('@actions/core/lib/command');
import { vi, describe, test, expect } from 'vitest';
vi.mock('@actions/core');
vi.mock('@actions/core/lib/command');
import * as core from '@actions/core';
import got from 'got';
@ -102,7 +104,7 @@ describe('authenticate with approle', () => {
});
beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
when(core.getInput)
.calledWith('method', expect.anything())

View file

@ -1,5 +1,7 @@
jest.mock('@actions/core');
jest.mock('@actions/core/lib/command');
import { vi, describe, test, expect } from 'vitest';
vi.mock('@actions/core');
vi.mock('@actions/core/lib/command');
import * as core from '@actions/core';
import got from 'got';
@ -107,7 +109,7 @@ describe('integration', () => {
});
beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
when(core.getInput)
.calledWith('url', expect.anything())

View file

@ -1,3 +0,0 @@
module.exports = {
verbose: true
};

View file

@ -1,12 +1,14 @@
jest.mock('@actions/core');
jest.mock('@actions/core/lib/command');
import { vi, describe, test, expect } from 'vitest';
vi.mock('@actions/core');
vi.mock('@actions/core/lib/command');
import * as core from '@actions/core';
import rsasign from 'jsrsasign';
import {
privateRsaKey,
privateRsaKeyBase64,
publicRsaKey
} from './rsa_keys.js');
} from './rsa_keys.js';
import got from 'got';
import { when } from 'jest-when'
@ -139,7 +141,7 @@ describe('jwt auth', () => {
describe('authenticate with private key', () => {
beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
when(core.getInput)
.calledWith('url', expect.anything())
@ -189,7 +191,7 @@ describe('jwt auth', () => {
})
beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
when(core.getInput)
.calledWith('url', expect.anything())

View file

@ -1,5 +1,7 @@
jest.mock('@actions/core');
jest.mock('@actions/core/lib/command');
import { vi, describe, test, expect } from 'vitest';
vi.mock('@actions/core');
vi.mock('@actions/core/lib/command');
import * as core from '@actions/core';
import got from 'got';

View file

@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
// required to make jest-when work with vitest
globals: true,
},
})

View file

@ -1,3 +1,5 @@
import { describe, test, expect } from 'vitest';
describe('e2e-tls', () => {
it('verify', () => {
expect(process.env.SECRET).toBe("SUPERSECRET");

View file

@ -1,3 +1,5 @@
import { describe, test, expect } from 'vitest';
describe('e2e', () => {
it('verify', () => {
expect(process.env.SECRET).toBe("SUPERSECRET");

View file

@ -1,3 +0,0 @@
module.exports = {
verbose: true
};

View file

@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
// required to make jest-when work with vitest
globals: true,
},
})

View file

@ -1,5 +1,7 @@
jest.mock('@actions/core');
jest.mock('@actions/core/lib/command');
import { vi, describe, test, expect } from 'vitest';
vi.mock('@actions/core');
vi.mock('@actions/core/lib/command');
import * as core from '@actions/core';
import got from 'got';
@ -41,7 +43,7 @@ describe('integration', () => {
});
beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
when(core.getInput)
.calledWith('url', expect.anything())
@ -238,7 +240,7 @@ describe('authenticate with approle', () => {
});
beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
when(core.getInput)
.calledWith('method', expect.anything())

View file

@ -1,3 +0,0 @@
module.exports = {
verbose: true
};

View file

@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
// required to make jest-when work with vitest
globals: true,
},
})

View file

@ -1,9 +0,0 @@
// module.exports = {
// transform: {},
// testPathIgnorePatterns: ['/node_modules/', '<rootDir>/integrationTests/'],
// };
export default {
transform: {},
testPathIgnorePatterns: ['/node_modules/', '<rootDir>/integrationTests/'],
};

2081
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -6,11 +6,11 @@
"main": "dist/index.js",
"scripts": {
"build": "ncc build src/entry.js -o dist",
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest",
"test:integration:basic": "jest -c integrationTests/basic/jest.config.js",
"test:integration:enterprise": "jest -c integrationTests/enterprise/jest.config.js",
"test:integration:e2e": "jest -c integrationTests/e2e/jest.config.js",
"test:integration:e2e-tls": "jest -c integrationTests/e2e-tls/jest.config.js"
"test": "vitest run retries",
"test:integration:basic": "vitest run -c integrationTests/basic/vitest.config.js",
"test:integration:enterprise": "vitest run -c integrationTests/enterprise/vitest.config.js",
"test:integration:e2e": "vitest run -c integrationTests/e2e/vitest.config.js",
"test:integration:e2e-tls": "vitest run -c integrationTests/e2e-tls/vitest.config.js"
},
"files": [
"src/**/*",
@ -47,6 +47,7 @@
"@vercel/ncc": "^0.38.1",
"jest": "^29.7.0",
"jest-when": "^3.6.0",
"mock-http-server": "^1.4.5"
"mock-http-server": "^1.4.5",
"vitest": "^1.3.1"
}
}

View file

@ -111,8 +111,7 @@ async function exportSecrets() {
for (const line of value.replace(/\r/g, '').split('\n')) {
if (line.length > 0) {
// core.setSecret(line);
core.setOutput(line);
core.setSecret(line);
}
}
if (exportEnv) {

View file

@ -1,8 +1,8 @@
import { jest } from '@jest/globals';
import { vi, describe, test, expect } from 'vitest';
jest.mock('got');
jest.mock('@actions/core');
jest.mock('@actions/core/lib/command');
vi.mock('got');
vi.mock('@actions/core');
vi.mock('@actions/core/lib/command');
import * as command from '@actions/core/lib/command';
import * as core from '@actions/core';
@ -134,7 +134,7 @@ describe('parseHeaders', () => {
describe('exportSecrets', () => {
beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
when(core.getInput)
.calledWith('url', expect.anything())

View file

@ -1,12 +1,12 @@
import { jest } from '@jest/globals';
import { vi, describe, test, expect } from 'vitest';
jest.mock('got');
jest.mock('@actions/core');
jest.mock('@actions/core/lib/command');
jest.mock('fs', () => ({
stat: jest.fn().mockResolvedValue(null),
vi.mock('got');
vi.mock('@actions/core');
vi.mock('@actions/core/lib/command');
vi.mock('fs', () => ({
stat: vi.fn().mockResolvedValue(null),
promises: {
access: jest.fn().mockResolvedValue(null),
access: vi.fn().mockResolvedValue(null),
}
}));
@ -26,7 +26,7 @@ function mockInput(name, key) {
function mockApiResponse() {
const response = { body: { auth: { client_token: testToken, renewable: true, policies: [], accessor: "accessor" } } }
got.post = jest.fn()
got.post = vi.fn()
got.post.mockReturnValue(response)
}
const testToken = "testoken";
@ -34,7 +34,7 @@ const testToken = "testoken";
describe("test retrival for token", () => {
beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
});
it("test retrival with approle", async () => {
@ -75,7 +75,7 @@ describe("test retrival for token", () => {
mockInput("kubernetesTokenPath", testTokenPath)
mockInput("role", testRole)
mockInput("path", testPath)
fs.readFileSync = jest.fn()
fs.readFileSync = vi.fn()
fs.readFileSync.mockReturnValueOnce(jwtToken)
const token = await retrieveToken(method, got)
expect(token).toEqual(testToken)

View file

@ -1,17 +1,19 @@
jest.mock('@actions/core');
import { vi, describe, test, expect } from 'vitest';
vi.mock('@actions/core');
import * as core from '@actions/core';
import * as ServerMock from 'mock-http-server';
import exportSecrets from './action.js';
import { when } from 'jest-when'
import ServerMock from 'mock-http-server';
import { exportSecrets } from './action.js';
import { when } from 'jest-when';
describe('exportSecrets retries', () => {
var server = new ServerMock({ host: "127.0.0.1", port: 0 });
var calls = 0;
beforeEach((done) => {
beforeEach(() => new Promise(done => {
calls = 0;
jest.resetAllMocks();
vi.resetAllMocks();
when(core.getInput)
.calledWith('token', expect.anything())
@ -28,11 +30,11 @@ describe('exportSecrets retries', () => {
.mockReturnValueOnce('http://127.0.0.1:' + server.getHttpPort());
done();
});
});
}));
afterEach((done) => {
afterEach(() => new Promise(done => {
server.stop(done);
});
}));
function mockStatusCodes(statusCodes) {
server.on({
@ -51,19 +53,19 @@ describe('exportSecrets retries', () => {
});
}
it('retries on 412 status code', (done) => {
it('retries on 412 status code', () => new Promise(done => {
mockStatusCodes([412, 200])
exportSecrets().then(() => {
expect(calls).toEqual(2);
done();
});
});
}));
it('retries on 500 status code', (done) => {
it('retries on 500 status code', () => new Promise(done => {
mockStatusCodes([500, 200])
exportSecrets().then(() => {
expect(calls).toEqual(2);
done();
});
});
}));
});

12
vitest.config.js Normal file
View file

@ -0,0 +1,12 @@
import { defineConfig, configDefaults } from 'vitest/config'
export default defineConfig({
test: {
// required to make jest-when work with vitest
globals: true,
exclude:[
...configDefaults.exclude,
'**/integrationTests/**'
]
},
})