Skip to content

Commit

Permalink
chore: 🔥 remove duplicated cases
Browse files Browse the repository at this point in the history
remove duplicated cases on test to avoid duplicated code to maintain
  • Loading branch information
equiman committed Oct 19, 2023
1 parent 89832e7 commit 047cef5
Show file tree
Hide file tree
Showing 19 changed files with 141 additions and 195 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ jobs:
-Dsonar.projectVersion=${{ steps.package.outputs.version }}
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
-Dsonar.coverage.exclusions=**/*.test.ts
-sonar.cpd.exclusions=**/*.test.ts
9 changes: 2 additions & 7 deletions src/alias/sa.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'
import { addCases } from '../cli/swpm/commands/add.test'

describe('sa', () => {
it.each([
['npm', 'npm add vite'],
['yarn', 'yarn add vite'],
['yarn@berry', 'yarn add vite'],
['pnpm', 'pnpm add vite'],
['bun', 'bun add vite']
])('%s', (pkg, expected) => {
it.each(addCases)('%s', (pkg, expected) => {
const result = testCommandResult(`sa vite --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
9 changes: 2 additions & 7 deletions src/alias/sad.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'
import { addDevCases } from '../cli/swpm/commands/add.test'

describe('sad', () => {
it.each([
['npm', 'npm add vite --save-dev'],
['yarn', 'yarn add vite --dev'],
['yarn@berry', 'yarn add vite --dev'],
['pnpm', 'pnpm add vite --save-dev'],
['bun', 'bun add vite --dev']
])('%s', (pkg, expected) => {
it.each(addDevCases)('%s', (pkg, expected) => {
const result = testCommandResult(`sad vite --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
9 changes: 2 additions & 7 deletions src/alias/sae.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'
import { addSaveExact } from '../cli/swpm/commands/add.test'

describe('sae', () => {
it.each([
['npm', 'npm add vite --save-exact'],
['yarn', 'yarn add vite --exact'],
['yarn@berry', 'yarn add vite --exact'],
['pnpm', 'pnpm add vite --save-exact'],
['bun', 'bun add vite --exact']
])('%s', (pkg, expected) => {
it.each(addSaveExact)('%s', (pkg, expected) => {
const result = testCommandResult(`sae vite --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
19 changes: 2 additions & 17 deletions src/alias/sag.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'
import { addPackageCases, addVoltaCases } from '../cli/swpm/commands/add.test'

describe('sag', () => {
const voltaCases = [
['npm', 'volta install vite'],
['yarn', 'volta install vite'],
['yarn@berry', 'volta install vite'],
['pnpm', 'volta install vite'],
['bun', 'volta install vite']
]

const packageCases = [
['npm', 'npm add vite --location=global'],
['yarn', 'yarn global add vite'],
['yarn@berry', 'yarn global add vite'],
['pnpm', 'pnpm add vite --global'],
['bun', 'bun add vite --global']
]

const voltaVersion = testCommandResult('volta --version')

it.each(voltaVersion ? voltaCases : packageCases)('%s', (pkg, expected) => {
it.each(voltaVersion ? addVoltaCases : addPackageCases)('%s', (pkg, expected) => {
const result = testCommandResult(`sag vite --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
25 changes: 4 additions & 21 deletions src/alias/si.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,23 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'
import { installCases, installFrozenCases, installLockCases } from '../cli/swpm/commands/install.test'

describe('si', () => {
it.each([
['npm', 'npm install'],
['yarn', 'yarn install'],
['yarn@berry', 'yarn install'],
['pnpm', 'pnpm install'],
['bun', 'bun install']
])('%s', (pkg, expected) => {
it.each(installCases)('%s', (pkg, expected) => {
const result = testCommandResult(`si --test ${pkg}`)
expect(result).toBe(expected)
})
})

describe('si --frozen', () => {
it.each([
['npm', 'npm ci'],
['yarn', 'yarn install --frozen-lockfile'],
['yarn@berry', 'yarn install --immutable'],
['pnpm', 'pnpm install --frozen-lockfile'],
['bun', 'bun install --frozen-lockfile']
])('%s', (pkg, expected) => {
it.each(installFrozenCases)('%s', (pkg, expected) => {
const result = testCommandResult(`si --frozen --test ${pkg}`)
expect(result).toBe(expected)
})
})

describe('si --package-lock', () => {
it.each([
['npm', 'npm install --no-package-lock'],
['yarn', 'yarn install --no-lockfile'],
['yarn@berry', 'yarn install --no-lockfile'],
['pnpm', ''], // not available
['bun', 'bun install --no-save']
])('%s', (pkg, expected) => {
it.each(installLockCases)('%s', (pkg, expected) => {
const result = testCommandResult(`si --package-lock --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
9 changes: 2 additions & 7 deletions src/alias/sif.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'
import { installFrozenCases } from '../cli/swpm/commands/install.test'

describe('sif', () => {
it.each([
['npm', 'npm ci'],
['yarn', 'yarn install --frozen-lockfile'],
['yarn@berry', 'yarn install --immutable'],
['pnpm', 'pnpm install --frozen-lockfile'],
['bun', 'bun install --frozen-lockfile']
])('%s', (pkg, expected) => {
it.each(installFrozenCases)('%s', (pkg, expected) => {
const result = testCommandResult(`sif --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
2 changes: 1 addition & 1 deletion src/alias/sr.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'

describe('sif', () => {
describe('sr', () => {
it.each([
['npm', 'npm run dev -- --port 3030'],
['yarn', 'yarn run dev --port 3030'],
Expand Down
9 changes: 2 additions & 7 deletions src/alias/srg.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'
import { removeGlobalCases } from '../cli/swpm/commands/remove.test'

describe('srg', () => {
it.each([
['npm', 'npm uninstall vite --location=global'],
['yarn', 'yarn global remove vite'],
['yarn@berry', 'yarn global remove vite'],
['pnpm', 'pnpm uninstall vite --global'],
['bun', 'bun remove vite --global']
])('%s', (pkg, expected) => {
it.each(removeGlobalCases)('%s', (pkg, expected) => {
const result = testCommandResult(`srg vite --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
9 changes: 2 additions & 7 deletions src/alias/srm.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'
import { removeCases } from '../cli/swpm/commands/remove.test'

describe('srm', () => {
it.each([
['npm', 'npm uninstall vite'],
['yarn', 'yarn remove vite'],
['yarn@berry', 'yarn remove vite'],
['pnpm', 'pnpm uninstall vite'],
['bun', 'bun remove vite']
])('%s', (pkg, expected) => {
it.each(removeCases)('%s', (pkg, expected) => {
const result = testCommandResult(`srm vite --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
9 changes: 2 additions & 7 deletions src/alias/sug.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'
import { upgradeCases } from '../cli/swpm/commands/upgrade.test'

describe('sug', () => {
it.each([
['npm', 'npm add vite@latest'],
['yarn', 'yarn upgrade vite --latest'],
['yarn@berry', 'yarn up vite'],
['pnpm', 'pnpm update vite --latest'],
['bun', '']
])('%s', (pkg, expected) => {
it.each(upgradeCases)('%s', (pkg, expected) => {
const result = testCommandResult(`sug vite --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
9 changes: 2 additions & 7 deletions src/alias/sui.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'
import { interactiveCases } from '../cli/swpm/commands/interactive.test'

describe('sui', () => {
it.each([
['npm', ''],
['yarn', 'yarn upgrade-interactive'],
['yarn@berry', 'yarn upgrade-interactive'],
['pnpm', 'pnpm upgrade --interactive'],
['bun', '']
])('%s', (pkg, expected) => {
it.each(interactiveCases)('%s', (pkg, expected) => {
const result = testCommandResult(`sui --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
9 changes: 2 additions & 7 deletions src/alias/sup.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'
import { updateCases } from '../cli/swpm/commands/update.test'

describe('sup', () => {
it.each([
['npm', 'npm update vite'],
['yarn', 'yarn upgrade vite'],
['yarn@berry', 'yarn semver up vite'],
['pnpm', 'pnpm update vite'],
['bun', 'bun update vite']
])('%s', (pkg, expected) => {
it.each(updateCases)('%s', (pkg, expected) => {
const result = testCommandResult(`sup vite --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
80 changes: 43 additions & 37 deletions src/cli/swpm/commands/add.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../../../.vitest/helpers'

export const addCases = [
['npm', 'npm add vite'],
['yarn', 'yarn add vite'],
['yarn@berry', 'yarn add vite'],
['pnpm', 'pnpm add vite'],
['bun', 'bun add vite']
]

describe('add', () => {
it.each([
['npm', 'npm add vite'],
['yarn', 'yarn add vite'],
['yarn@berry', 'yarn add vite'],
['pnpm', 'pnpm add vite'],
['bun', 'bun add vite']
])('%s', (pkg, expected) => {
it.each(addCases)('%s', (pkg, expected) => {
const result = testCommandResult(`swpm add vite --test ${pkg}`)
expect(result).toBe(expected)
})
})

export const addDevCases = [
['npm', 'npm add vite --save-dev'],
['yarn', 'yarn add vite --dev'],
['yarn@berry', 'yarn add vite --dev'],
['pnpm', 'pnpm add vite --save-dev'],
['bun', 'bun add vite --dev']
]

describe('add --save-dev', () => {
it.each([
['npm', 'npm add vite --save-dev'],
['yarn', 'yarn add vite --dev'],
['yarn@berry', 'yarn add vite --dev'],
['pnpm', 'pnpm add vite --save-dev'],
['bun', 'bun add vite --dev']
])('%s', (pkg, expected) => {
it.each(addDevCases)('%s', (pkg, expected) => {
const result = testCommandResult(`swpm add vite --save-dev --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down Expand Up @@ -53,39 +57,41 @@ describe('add --save-peer', () => {
})
})

export const addSaveExact = [
['npm', 'npm add vite --save-exact'],
['yarn', 'yarn add vite --exact'],
['yarn@berry', 'yarn add vite --exact'],
['pnpm', 'pnpm add vite --save-exact'],
['bun', 'bun add vite --exact']
]

describe('add --save-exact', () => {
it.each([
['npm', 'npm add vite --save-exact'],
['yarn', 'yarn add vite --exact'],
['yarn@berry', 'yarn add vite --exact'],
['pnpm', 'pnpm add vite --save-exact'],
['bun', 'bun add vite --exact']
])('%s', (pkg, expected) => {
it.each(addSaveExact)('%s', (pkg, expected) => {
const result = testCommandResult(`swpm add vite --save-exact --test ${pkg}`)
expect(result).toBe(expected)
})
})

describe('add --global', () => {
const voltaCases = [
['npm', 'volta install vite'],
['yarn', 'volta install vite'],
['yarn@berry', 'volta install vite'],
['pnpm', 'volta install vite'],
['bun', 'volta install vite']
]
export const addVoltaCases = [
['npm', 'volta install vite'],
['yarn', 'volta install vite'],
['yarn@berry', 'volta install vite'],
['pnpm', 'volta install vite'],
['bun', 'volta install vite']
]

const packageCases = [
['npm', 'npm add vite --location=global'],
['yarn', 'yarn global add vite'],
['yarn@berry', 'yarn global add vite'],
['pnpm', 'pnpm add vite --global'],
['bun', 'bun add vite --global']
]
export const addPackageCases = [
['npm', 'npm add vite --location=global'],
['yarn', 'yarn global add vite'],
['yarn@berry', 'yarn global add vite'],
['pnpm', 'pnpm add vite --global'],
['bun', 'bun add vite --global']
]

describe('add --global', () => {
const voltaVersion = testCommandResult('volta --version')

it.each(voltaVersion ? voltaCases : packageCases)('%s', (pkg, expected) => {
it.each(voltaVersion ? addVoltaCases : addPackageCases)('%s', (pkg, expected) => {
const result = testCommandResult(`swpm add vite --global --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
Loading

0 comments on commit 047cef5

Please sign in to comment.