|
| 1 | +import fs from 'fs/promises'; |
| 2 | +import * as exec from '@actions/exec'; |
| 3 | +import { describe, expect, it, test, vi } from 'vitest'; |
| 4 | +import { checkForChanges, getPackageInfo } from '../index.js'; |
| 5 | + |
| 6 | +const mockedExecOutput = vi.spyOn(exec, 'getExecOutput'); |
| 7 | +const mockedFsReadFile = vi.spyOn(fs, 'readFile'); |
| 8 | + |
| 9 | +describe('Test checkForChanges', () => { |
| 10 | + it('should return true if program exits with non zero code', async () => { |
| 11 | + mockedExecOutput.mockResolvedValueOnce({ |
| 12 | + exitCode: 1, stdout: '', stderr: '' |
| 13 | + }); |
| 14 | + |
| 15 | + await expect(checkForChanges('/')).resolves.toEqual(true); |
| 16 | + expect(mockedExecOutput).toHaveBeenCalledOnce(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should return false if program exits with 0', async () => { |
| 20 | + mockedExecOutput.mockResolvedValueOnce({ |
| 21 | + exitCode: 0, stdout: '', stderr: '' |
| 22 | + }); |
| 23 | + |
| 24 | + await expect(checkForChanges('/')).resolves.toEqual(false); |
| 25 | + expect(mockedExecOutput).toHaveBeenCalledOnce(); |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +describe('Test getPackageInfo', () => { |
| 30 | + /** |
| 31 | + * Use for momentarily mocking the loading of a package.json |
| 32 | + * file |
| 33 | + */ |
| 34 | + function mockPackageJson(name: string, needsPlaywright: boolean, changes: boolean) { |
| 35 | + const data: Record<string, unknown> = { |
| 36 | + name |
| 37 | + }; |
| 38 | + |
| 39 | + if (needsPlaywright) { |
| 40 | + data.devDependencies = { |
| 41 | + playwright: '^1.54.0' |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + mockedFsReadFile.mockResolvedValueOnce(JSON.stringify(data)); |
| 46 | + mockedExecOutput.mockResolvedValueOnce({ |
| 47 | + exitCode: changes ? 1 : 0, stdout: '', stderr: '' |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + test('regular loading of bundle', async () => { |
| 52 | + mockPackageJson('@sourceacademy/bundle-bundle0', false, true); |
| 53 | + |
| 54 | + return expect(getPackageInfo('/')).resolves.toMatchObject({ |
| 55 | + directory: '/', |
| 56 | + changes: true, |
| 57 | + needsPlaywright: false, |
| 58 | + bundleName: 'bundle0' |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + test('regular loading of tab without playwright', () => { |
| 63 | + mockPackageJson('@sourceacademy/tab-Tab0', false, true); |
| 64 | + return expect(getPackageInfo('/')).resolves.toMatchObject({ |
| 65 | + directory: '/', |
| 66 | + changes: true, |
| 67 | + needsPlaywright: false, |
| 68 | + tabName: 'Tab0' |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + test('regular loading of a library without playwright', () => { |
| 73 | + mockPackageJson('@sourceacademy/modules-lib', false, true); |
| 74 | + return expect(getPackageInfo('/')).resolves.toMatchObject({ |
| 75 | + directory: '/', |
| 76 | + changes: true, |
| 77 | + needsPlaywright: false, |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + test('loading a library without changes', () => { |
| 82 | + mockPackageJson('@sourceacademy/modules-lib', false, false); |
| 83 | + return expect(getPackageInfo('/')).resolves.toMatchObject({ |
| 84 | + directory: '/', |
| 85 | + changes: false, |
| 86 | + needsPlaywright: false, |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + test('loading a library that needs playwright', () => { |
| 91 | + mockedExecOutput.mockResolvedValueOnce({ |
| 92 | + exitCode: 1, stdout: '', stderr: '' |
| 93 | + }); |
| 94 | + |
| 95 | + mockPackageJson('@sourceacademy/modules-lib', true, true); |
| 96 | + return expect(getPackageInfo('/')).resolves.toMatchObject({ |
| 97 | + directory: '/', |
| 98 | + changes: true, |
| 99 | + needsPlaywright: true, |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + test('loading an unknown package', () => { |
| 104 | + mockPackageJson('unknown-package', false, false); |
| 105 | + return expect(getPackageInfo('/')).rejects.toThrowError('Failed to match package name: unknown-package'); |
| 106 | + }); |
| 107 | +}); |
0 commit comments