refactor(testing): migrate from Jest to Vitest for testing framework
This commit is contained in:
@@ -3,17 +3,17 @@ import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import {Audit} from '../src/audit'
|
||||
|
||||
jest.mock('child_process')
|
||||
vi.mock('child_process')
|
||||
|
||||
const audit = new Audit()
|
||||
|
||||
describe('run', () => {
|
||||
beforeEach(() => {
|
||||
jest.mocked(child_process).spawnSync.mockClear()
|
||||
vi.mocked(child_process).spawnSync.mockClear()
|
||||
})
|
||||
|
||||
test('finds vulnerabilities with default values', () => {
|
||||
jest.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
vi.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
const stdout = fs.readFileSync(
|
||||
path.join(__dirname, 'testdata/audit/error.txt')
|
||||
)
|
||||
@@ -34,7 +34,7 @@ describe('run', () => {
|
||||
})
|
||||
|
||||
test('finds vulnerabilities with production flag enabled', () => {
|
||||
jest.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
vi.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
const stdout = fs.readFileSync(
|
||||
path.join(__dirname, 'testdata/audit/error.txt')
|
||||
)
|
||||
@@ -55,7 +55,7 @@ describe('run', () => {
|
||||
})
|
||||
|
||||
test('finds vulnerabilities with json flag enabled', () => {
|
||||
jest.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
vi.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
const stdout = fs.readFileSync(
|
||||
path.join(__dirname, 'testdata/audit/error.json')
|
||||
)
|
||||
@@ -76,7 +76,7 @@ describe('run', () => {
|
||||
})
|
||||
|
||||
test('does not find vulnerabilities', () => {
|
||||
jest.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
vi.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
const stdout = fs.readFileSync(
|
||||
path.join(__dirname, 'testdata/audit/success.txt')
|
||||
)
|
||||
@@ -97,7 +97,7 @@ describe('run', () => {
|
||||
})
|
||||
|
||||
test('throws an error if error is not null', () => {
|
||||
jest.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
vi.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
return {
|
||||
pid: 100,
|
||||
output: '',
|
||||
@@ -115,7 +115,7 @@ describe('run', () => {
|
||||
})
|
||||
|
||||
test('throws an error if status is null', () => {
|
||||
jest.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
vi.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
return {
|
||||
pid: 100,
|
||||
output: '',
|
||||
@@ -133,7 +133,7 @@ describe('run', () => {
|
||||
})
|
||||
|
||||
test('throws an error if stderr is null', () => {
|
||||
jest.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
vi.mocked(child_process).spawnSync.mockImplementation((): any => {
|
||||
return {
|
||||
pid: 100,
|
||||
output: '',
|
||||
|
||||
@@ -68,7 +68,7 @@ describe('getExistingIssueNumber', () => {
|
||||
})
|
||||
|
||||
test('gets existing open issue', async () => {
|
||||
const getIssues = jest.fn()
|
||||
const getIssues = vi.fn()
|
||||
getIssues.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
@@ -89,7 +89,7 @@ describe('getExistingIssueNumber', () => {
|
||||
})
|
||||
|
||||
test('returns null when there is no open issue', async () => {
|
||||
const getIssues = jest.fn()
|
||||
const getIssues = vi.fn()
|
||||
getIssues.mockResolvedValue({data: []})
|
||||
|
||||
const result = await issue.getExistingIssueNumber(getIssues, {repo, owner})
|
||||
@@ -104,7 +104,7 @@ describe('getExistingIssueNumber', () => {
|
||||
})
|
||||
|
||||
test('returns null when no issues match the issue title', async () => {
|
||||
const getIssues = jest.fn()
|
||||
const getIssues = vi.fn()
|
||||
getIssues.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
|
||||
@@ -5,17 +5,17 @@ import {run} from '../src/main'
|
||||
import * as issue from '../src/issue'
|
||||
import * as pr from '../src/pr'
|
||||
|
||||
jest.mock('../src/audit')
|
||||
jest.mock('../src/issue')
|
||||
jest.mock('../src/pr')
|
||||
jest.mock('@octokit/rest', () => {
|
||||
vi.mock('../src/audit')
|
||||
vi.mock('../src/issue')
|
||||
vi.mock('../src/pr')
|
||||
vi.mock('@octokit/rest', () => {
|
||||
return {
|
||||
Octokit: jest.fn().mockImplementation(() => {
|
||||
Octokit: vi.fn().mockImplementation(() => {
|
||||
return {
|
||||
issues: {
|
||||
listForRepo: jest.fn(),
|
||||
createComment: jest.fn(),
|
||||
create: jest.fn()
|
||||
listForRepo: vi.fn(),
|
||||
createComment: vi.fn(),
|
||||
create: vi.fn()
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -25,8 +25,8 @@ jest.mock('@octokit/rest', () => {
|
||||
describe('run: pr', () => {
|
||||
beforeEach(() => {
|
||||
// initialize mock
|
||||
jest.mocked(Audit).mockClear()
|
||||
jest.mocked(pr).createComment.mockClear()
|
||||
vi.mocked(Audit).mockClear()
|
||||
vi.mocked(pr).createComment.mockClear()
|
||||
|
||||
process.env.INPUT_AUDIT_LEVEL = 'low'
|
||||
process.env.INPUT_PRODUCTION_FLAG = 'false'
|
||||
@@ -39,7 +39,7 @@ describe('run: pr', () => {
|
||||
})
|
||||
|
||||
test('does not call pr.createComment if vulnerabilities are not found', () => {
|
||||
jest.mocked(Audit).mockImplementation((): any => {
|
||||
vi.mocked(Audit).mockImplementation((): any => {
|
||||
return {
|
||||
stdout: fs.readFileSync(
|
||||
path.join(__dirname, 'testdata/audit/success.txt')
|
||||
@@ -57,14 +57,14 @@ describe('run: pr', () => {
|
||||
}
|
||||
})
|
||||
|
||||
jest.mocked(pr).createComment.mockResolvedValue()
|
||||
vi.mocked(pr).createComment.mockResolvedValue()
|
||||
|
||||
expect(run).not.toThrowError()
|
||||
expect(pr.createComment).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('calls pr.createComment if vulnerabilities are found in PR', () => {
|
||||
jest.mocked(Audit).mockImplementation((): any => {
|
||||
vi.mocked(Audit).mockImplementation((): any => {
|
||||
return {
|
||||
stdout: fs.readFileSync(
|
||||
path.join(__dirname, 'testdata/audit/error.txt')
|
||||
@@ -82,7 +82,7 @@ describe('run: pr', () => {
|
||||
}
|
||||
})
|
||||
|
||||
jest.mocked(pr).createComment.mockResolvedValue()
|
||||
vi.mocked(pr).createComment.mockResolvedValue()
|
||||
|
||||
expect(run).not.toThrowError()
|
||||
expect(pr.createComment).toHaveBeenCalled()
|
||||
@@ -91,7 +91,7 @@ describe('run: pr', () => {
|
||||
test('does not call pr.createComment if create_pr_comments is set to false', () => {
|
||||
process.env.INPUT_CREATE_PR_COMMENTS = 'false'
|
||||
|
||||
jest.mocked(Audit).mockImplementation((): any => {
|
||||
vi.mocked(Audit).mockImplementation((): any => {
|
||||
return {
|
||||
stdout: fs.readFileSync(
|
||||
path.join(__dirname, 'testdata/audit/error.txt')
|
||||
@@ -117,8 +117,8 @@ describe('run: pr', () => {
|
||||
describe('run: issue', () => {
|
||||
beforeEach(() => {
|
||||
// initialize mock
|
||||
jest.mocked(Audit).mockClear()
|
||||
jest.mocked(issue).getExistingIssueNumber.mockClear()
|
||||
vi.mocked(Audit).mockClear()
|
||||
vi.mocked(issue).getExistingIssueNumber.mockClear()
|
||||
|
||||
process.env.INPUT_AUDIT_LEVEL = 'low'
|
||||
process.env.INPUT_PRODUCTION_FLAG = 'false'
|
||||
@@ -133,7 +133,7 @@ describe('run: issue', () => {
|
||||
test('does not call octokit.rest.issues.create if create_issues is set to false', () => {
|
||||
process.env.INPUT_CREATE_ISSUES = 'false'
|
||||
|
||||
jest.mocked(Audit).mockImplementation((): any => {
|
||||
vi.mocked(Audit).mockImplementation((): any => {
|
||||
return {
|
||||
stdout: fs.readFileSync(
|
||||
path.join(__dirname, 'testdata/audit/error.txt')
|
||||
@@ -151,7 +151,7 @@ describe('run: issue', () => {
|
||||
}
|
||||
})
|
||||
|
||||
jest.mocked(issue).getExistingIssueNumber.mockResolvedValue(null)
|
||||
vi.mocked(issue).getExistingIssueNumber.mockResolvedValue(null)
|
||||
|
||||
expect(run).not.toThrowError()
|
||||
expect(issue.getExistingIssueNumber).not.toHaveBeenCalled()
|
||||
|
||||
Reference in New Issue
Block a user