Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {type Output} from '@sanity/cli-core'
import {type Output} from '@sanity/cli-core/types'
import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest'

import {buildApp, type BuildOptions} from '../buildApp.js'
Expand Down Expand Up @@ -39,6 +39,7 @@ const mockGetAppEnvironmentVariables = vi.hoisted(() => vi.fn().mockReturnValue(
const mockedIsInteractive = vi.hoisted(() => vi.fn(() => true))
const mockedBuildStaticFiles = vi.hoisted(() => vi.fn())
const mockedGetLocalPackageVersion = vi.hoisted(() => vi.fn())
const mockedResolveWorkbenchApp = vi.hoisted(() => vi.fn())

vi.mock(import('../buildStaticFiles.js'), () => ({
buildStaticFiles: mockedBuildStaticFiles,
Expand All @@ -48,21 +49,29 @@ vi.mock(import('../resolveVendorBuildConfig.js'), () => ({
resolveVendorBuildConfig: vi.fn(),
}))

vi.mock(import('../buildDebug'), () => ({
buildDebug: vi.fn() as unknown as (typeof import('../buildDebug'))['buildDebug'],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odd that you had to override the type in this case.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you pass in an import return type to vi.mock as the first parameter, then vi.mock can infer what the mock's shape should look like, so then you do need to type it. Can be helpful, or can be annoying. Sometimes it is easier to just pass a string. Sometimes if you want the test/mock to be kept up to date with module changes, it can be nice to get a type error if your mock drifts out of date. In this case, probably not worth it and should drop the import to ignore the types. I'll do that.

}))

vi.mock(import('../getEnvironmentVariables.js'), () => ({
getAppEnvironmentVariables: mockGetAppEnvironmentVariables,
}))

vi.mock(import('@sanity/cli-core'), async (importOriginal) => {
const original = await importOriginal()
return {
...original,
getLocalPackageVersion: mockedGetLocalPackageVersion,
isInteractive: mockedIsInteractive,
}
})
vi.mock(import('../getAutoUpdatesImportMap'), () => ({
getAutoUpdatesCssUrls: vi.fn(),
getAutoUpdatesImportMap: vi.fn(),
}))

vi.mock(import('@sanity/cli-core/package-manager'), () => ({
getLocalPackageVersion: mockedGetLocalPackageVersion,
}))

vi.mock(import('@sanity/cli-core/util'), () => ({
isInteractive: mockedIsInteractive,
}))

vi.mock(import('@sanity/cli-core/ux'), async (importOriginal) => {
const original = await importOriginal<typeof import('@sanity/cli-core/ux')>()
const original = await importOriginal()
mockedSpinner.mockImplementation(original.spinner)
return {
...original,
Expand All @@ -87,6 +96,7 @@ describe('#buildApp', () => {
mockedConfirm.mockResolvedValue(true)
mockedSelect.mockResolvedValue('disable-auto-updates')
mockedGetLocalPackageVersion.mockResolvedValue('1.0.0')
mockedResolveWorkbenchApp.mockReturnValue({})
})

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {convertToSystemPath} from '@sanity/cli-test'
import path from 'node:path'

import {type InlineConfig} from 'vite'
import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest'

Expand All @@ -14,14 +15,16 @@ const mockExtendViteConfigWithUserConfig = vi.hoisted(() => vi.fn())
const mockFinalizeViteConfig = vi.hoisted(() => vi.fn())
const mockWriteSanityRuntime = vi.hoisted(() => vi.fn())
const mockResolveEntries = vi.hoisted(() => vi.fn())
const mockCopyDir = vi.hoisted(() => vi.fn())
const mockWriteFavicons = vi.hoisted(() => vi.fn())

vi.mock('vite', () => ({
build: mockBuild,
createBuilder: mockCreateBuilder,
}))

vi.mock('../../../util/copyDir.js', () => ({
copyDir: vi.fn().mockResolvedValue(undefined),
copyDir: mockCopyDir,
}))

vi.mock('../buildDebug.js', () => ({
Expand All @@ -35,20 +38,20 @@ vi.mock('../getViteConfig.js', () => ({
}))

vi.mock('../writeFavicons.js', () => ({
writeFavicons: vi.fn().mockResolvedValue(undefined),
writeFavicons: mockWriteFavicons,
}))

vi.mock('../writeSanityRuntime.js', () => ({
resolveEntries: mockResolveEntries,
writeSanityRuntime: mockWriteSanityRuntime,
}))

const cwd = convertToSystemPath('/test/cwd')
const outputDir = convertToSystemPath('/test/cwd/dist')
const cwd = '/test/cwd'
const outputDir = '/test/cwd/dist'
const defaultViteConfig: InlineConfig = {plugins: [{name: 'sanity-default'}], root: cwd}

describe('buildStaticFiles', () => {
beforeEach(() => {
const defaultViteConfig: InlineConfig = {plugins: [{name: 'sanity-default'}], root: cwd}
mockGetViteConfig.mockResolvedValue(defaultViteConfig)
mockExtendViteConfigWithUserConfig.mockImplementation(async (_env, base, user) =>
typeof user === 'function' ? user(base, _env) : {...base, ...user},
Expand All @@ -67,7 +70,7 @@ describe('buildStaticFiles', () => {
vi.clearAllMocks()
})

describe('federation enabled', () => {
describe('federation enabled / isWorkbenchApp=true', () => {
test('applies user vite config so custom plugins run during build', async () => {
const userPlugin = {name: 'vanilla-extract-plugin'}
const userVite = vi.fn((config: InlineConfig) => ({
Expand Down Expand Up @@ -131,4 +134,44 @@ describe('buildStaticFiles', () => {
)
})
})
describe('isWorkbenchapp=false', () => {
test('should run a vite build, write favicons and return chunk stats', async () => {
const basePath = '/' // this is OS-agnostic, even on windows :shrug:
const staticOutputPath = path.join(outputDir, 'static')
const faviconBasePath = '/static'
mockBuild.mockResolvedValue({
output: [
{
modules: {
'first/path': {renderedLength: 420},
'second/path': {renderedLength: 69},
},
name: 'chonkiboi',
type: 'chunk',
},
{type: 'notchunk'},
],
})

const {chunks} = await buildStaticFiles({
basePath,
cwd,
isWorkbenchApp: false,
outputDir,
schemaExtraction: {enabled: true},
})

expect(mockCopyDir).toHaveBeenCalledWith(path.join(cwd, 'static'), staticOutputPath)
expect(mockWriteFavicons).toHaveBeenCalledWith(faviconBasePath, staticOutputPath)
Comment thread
cursor[bot] marked this conversation as resolved.
expect(mockBuild).toHaveBeenCalledWith(defaultViteConfig)
// Should ignore non-chunk types
expect(chunks).toHaveLength(1)
expect(chunks[0].name).toEqual('chonkiboi')
expect(chunks[0].modules).toHaveLength(2)
expect(chunks[0].modules[0].name).toEqual('first/path')
expect(chunks[0].modules[0].renderedLength).toEqual(420)
expect(chunks[0].modules[1].name).toEqual('second/path')
expect(chunks[0].modules[1].renderedLength).toEqual(69)
})
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {type Output} from '@sanity/cli-core'
import {type Output} from '@sanity/cli-core/types'
import {DefinedTelemetryTrace} from '@sanity/telemetry'
import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest'

Expand Down Expand Up @@ -80,14 +80,12 @@ vi.mock(import('../getEnvironmentVariables.js'), () => ({
getStudioEnvironmentVariables: mockGetStudioEnvironmentVariables,
}))

vi.mock(import('@sanity/cli-core'), async (importOriginal) => {
const original = await importOriginal()
return {
...original,
getLocalPackageVersion: mockGetLocalPackageVersion,
isInteractive: mockedIsInteractive,
}
})
vi.mock(import('@sanity/cli-core/package-manager'), () => ({
getLocalPackageVersion: mockGetLocalPackageVersion,
}))
vi.mock(import('@sanity/cli-core/util'), () => ({
isInteractive: mockedIsInteractive,
}))

vi.mock(import('@sanity/cli-core/ux'), async (importOriginal) => {
const original = await importOriginal()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import {type Output} from '@sanity/cli-core'
import {
getLocalPackageVersion as mockGetLocalPackageVersion,
readPackageJson as mockReadPackageJson,
} from '@sanity/cli-test/mocks/cli-core/package-manager'
import {createMockOutput} from '@sanity/cli-test/mocks/cli-core/SanityCommand'
import {beforeEach, describe, expect, test, vi} from 'vitest'

import {checkRequiredDependencies} from '../checkRequiredDependencies'

const mockReadPackageJson = vi.hoisted(() => vi.fn())
const mockedGetLocalPackageVersion = vi.hoisted(() => vi.fn())
vi.mock('semver', {spy: true})

vi.mock('@sanity/cli-core', async (importOriginal) => {
const actual = await importOriginal<typeof import('@sanity/cli-core')>()
return {
...actual,
getLocalPackageVersion: mockedGetLocalPackageVersion,
readPackageJson: mockReadPackageJson,
}
})
vi.mock(
'@sanity/cli-core/package-manager',
() => import('@sanity/cli-test/mocks/cli-core/package-manager'),
)

describe('#checkRequiredDependencies', () => {
const workDir = '/tmp/test-studio'
const mockOutput = {
error: vi.fn(),
log: vi.fn(),
print: vi.fn(),
warn: vi.fn(),
} as unknown as Output
const mockOutput = createMockOutput()

beforeEach(() => {
vi.clearAllMocks()
Expand All @@ -45,7 +39,7 @@ describe('#checkRequiredDependencies', () => {
name: 'test-studio',
version: '1.0.0',
})
mockedGetLocalPackageVersion.mockImplementation(async (module: string) => {
mockGetLocalPackageVersion.mockImplementation(async (module: string) => {
if (module === 'sanity') {
return null
}
Expand All @@ -71,7 +65,7 @@ describe('#checkRequiredDependencies', () => {
name: 'test-studio',
version: '1.0.0',
})
mockedGetLocalPackageVersion.mockImplementation(async (module: string) => {
mockGetLocalPackageVersion.mockImplementation(async (module: string) => {
if (module === 'sanity') {
return '3.0.0'
}
Expand All @@ -98,7 +92,7 @@ describe('#checkRequiredDependencies', () => {
name: 'test-studio',
version: '1.0.0',
})
mockedGetLocalPackageVersion.mockResolvedValue('3.0.0') // for sanity
mockGetLocalPackageVersion.mockResolvedValue('3.0.0') // for sanity

const result = await checkRequiredDependencies({
isApp: false,
Expand All @@ -122,7 +116,7 @@ describe('#checkRequiredDependencies', () => {
name: 'test-studio',
version: '1.0.0',
})
mockedGetLocalPackageVersion.mockResolvedValue('6.1.15')
mockGetLocalPackageVersion.mockResolvedValue('6.1.15')

await checkRequiredDependencies({
isApp: false,
Expand All @@ -144,7 +138,7 @@ describe('#checkRequiredDependencies', () => {
name: 'test-studio',
version: '1.0.0',
})
mockedGetLocalPackageVersion.mockResolvedValue('6.1.15')
mockGetLocalPackageVersion.mockResolvedValue('6.1.15')

await checkRequiredDependencies({
isApp: false,
Expand All @@ -162,7 +156,7 @@ describe('#checkRequiredDependencies', () => {
name: 'test-studio',
version: '1.0.0',
})
mockedGetLocalPackageVersion.mockImplementation(async (module: string) => {
mockGetLocalPackageVersion.mockImplementation(async (module: string) => {
if (module === 'styled-components') {
return null
}
Expand All @@ -189,7 +183,7 @@ describe('#checkRequiredDependencies', () => {
name: 'test-studio',
version: '1.0.0',
})
mockedGetLocalPackageVersion.mockImplementation(async (module: string) => {
mockGetLocalPackageVersion.mockImplementation(async (module: string) => {
if (module === 'styled-components') {
return '5.3.6'
}
Expand All @@ -216,7 +210,7 @@ describe('#checkRequiredDependencies', () => {
name: 'test-studio',
version: '1.0.0',
})
mockedGetLocalPackageVersion.mockImplementation(async (module: string) => {
mockGetLocalPackageVersion.mockImplementation(async (module: string) => {
if (module === 'sanity') return '3.2.0'
if (module === 'styled-components') return '6.1.15'
return null
Expand All @@ -240,7 +234,7 @@ describe('#checkRequiredDependencies', () => {
name: 'test-studio',
version: '1.0.0',
})
mockedGetLocalPackageVersion.mockImplementation(async (module: string) => {
mockGetLocalPackageVersion.mockImplementation(async (module: string) => {
if (module === 'sanity') return '3.2.0'
if (module === 'styled-components') return '6.1.15'
return null
Expand All @@ -264,7 +258,7 @@ describe('#checkRequiredDependencies', () => {
name: 'test-studio',
version: '1.0.0',
})
mockedGetLocalPackageVersion.mockImplementation(async (module: string) => {
mockGetLocalPackageVersion.mockImplementation(async (module: string) => {
if (module === 'sanity') return '3.2.0'
if (module === 'styled-components') return '5.3.6'
return null
Expand Down Expand Up @@ -292,7 +286,7 @@ describe('#checkRequiredDependencies', () => {
version: '1.0.0',
})

mockedGetLocalPackageVersion.mockImplementation(async (module: string) => {
mockGetLocalPackageVersion.mockImplementation(async (module: string) => {
if (module === 'sanity') return '3.2.0'
if (module === 'styled-components') return '6.1.15'
return null
Expand Down
Loading
Loading