|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { getDockerfileTemplate } from './dockerfile.js'; |
| 3 | +import { getDockerignoreTemplate } from './dockerignore.js'; |
| 4 | + |
| 5 | +describe('deployment templates', () => { |
| 6 | + describe('getDockerfileTemplate', () => { |
| 7 | + it('should use multi-stage build', () => { |
| 8 | + const template = getDockerfileTemplate(); |
| 9 | + expect(template).toContain('AS builder'); |
| 10 | + expect(template).toContain('AS production'); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should use Node 20 Alpine', () => { |
| 14 | + const template = getDockerfileTemplate(); |
| 15 | + expect(template).toContain('FROM node:20-alpine'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should copy dist folder from builder', () => { |
| 19 | + const template = getDockerfileTemplate(); |
| 20 | + expect(template).toContain('COPY --from=builder /app/dist ./dist'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should expose port 3000', () => { |
| 24 | + const template = getDockerfileTemplate(); |
| 25 | + expect(template).toContain('EXPOSE 3000'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should run node dist/index.js', () => { |
| 29 | + const template = getDockerfileTemplate(); |
| 30 | + expect(template).toContain('CMD ["node", "dist/index.js"]'); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('npm (default)', () => { |
| 34 | + it('should use npm ci for install', () => { |
| 35 | + const template = getDockerfileTemplate(); |
| 36 | + expect(template).toContain('RUN npm ci'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should use npm ci --omit=dev for production', () => { |
| 40 | + const template = getDockerfileTemplate(); |
| 41 | + expect(template).toContain('npm ci --omit=dev'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should copy package-lock.json', () => { |
| 45 | + const template = getDockerfileTemplate(); |
| 46 | + expect(template).toContain('COPY package.json package-lock.json'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should use npm run build', () => { |
| 50 | + const template = getDockerfileTemplate(); |
| 51 | + expect(template).toContain('RUN npm run build'); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('pnpm', () => { |
| 56 | + it('should use pnpm install --frozen-lockfile', () => { |
| 57 | + const template = getDockerfileTemplate({ packageManager: 'pnpm' }); |
| 58 | + expect(template).toContain('RUN pnpm install --frozen-lockfile'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should use pnpm install --frozen-lockfile --prod for production', () => { |
| 62 | + const template = getDockerfileTemplate({ packageManager: 'pnpm' }); |
| 63 | + expect(template).toContain('pnpm install --frozen-lockfile --prod'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should copy pnpm-lock.yaml', () => { |
| 67 | + const template = getDockerfileTemplate({ packageManager: 'pnpm' }); |
| 68 | + expect(template).toContain('COPY package.json pnpm-lock.yaml'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should enable corepack for pnpm', () => { |
| 72 | + const template = getDockerfileTemplate({ packageManager: 'pnpm' }); |
| 73 | + expect(template).toContain('corepack enable'); |
| 74 | + expect(template).toContain('corepack prepare pnpm'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should use pnpm run build', () => { |
| 78 | + const template = getDockerfileTemplate({ packageManager: 'pnpm' }); |
| 79 | + expect(template).toContain('RUN pnpm run build'); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('yarn', () => { |
| 84 | + it('should use yarn install --frozen-lockfile', () => { |
| 85 | + const template = getDockerfileTemplate({ packageManager: 'yarn' }); |
| 86 | + expect(template).toContain('RUN yarn install --frozen-lockfile'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should use yarn install --frozen-lockfile --production for production', () => { |
| 90 | + const template = getDockerfileTemplate({ packageManager: 'yarn' }); |
| 91 | + expect(template).toContain('yarn install --frozen-lockfile --production'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should copy yarn.lock', () => { |
| 95 | + const template = getDockerfileTemplate({ packageManager: 'yarn' }); |
| 96 | + expect(template).toContain('COPY package.json yarn.lock'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should enable corepack for yarn', () => { |
| 100 | + const template = getDockerfileTemplate({ packageManager: 'yarn' }); |
| 101 | + expect(template).toContain('corepack enable'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should use yarn build', () => { |
| 105 | + const template = getDockerfileTemplate({ packageManager: 'yarn' }); |
| 106 | + expect(template).toContain('RUN yarn build'); |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('getDockerignoreTemplate', () => { |
| 112 | + it('should ignore node_modules', () => { |
| 113 | + const template = getDockerignoreTemplate(); |
| 114 | + expect(template).toContain('node_modules/'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should ignore dist folder', () => { |
| 118 | + const template = getDockerignoreTemplate(); |
| 119 | + expect(template).toContain('dist/'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should ignore .env files', () => { |
| 123 | + const template = getDockerignoreTemplate(); |
| 124 | + expect(template).toContain('.env'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should ignore .git folder', () => { |
| 128 | + const template = getDockerignoreTemplate(); |
| 129 | + expect(template).toContain('.git/'); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments