Skip to content

Commit

Permalink
chore(tests): re-implement basic tests (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Aug 23, 2022
1 parent f13127f commit c6c103b
Show file tree
Hide file tree
Showing 12 changed files with 840 additions and 69 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ jobs:
if: steps.cache.outputs.cache-hit != 'true'
run: yarn

- name: Prepare
run: yarn dev:prepare

- name: Lint
run: yarn lint

# - name: Test
# run: yarn test
- name: Test
run: yarn test

# - name: Coverage
# run: yarn codecov
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"lint": "eslint --ext .js,.ts,.vue",
"prepack": "yarn build",
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint"
"test": "vitest"
},
"dependencies": {
"@nuxt/kit": "^3.0.0-rc.5",
Expand All @@ -42,13 +42,15 @@
},
"devDependencies": {
"@fontsource/inter": "^4.5.11",
"@nuxt/content": "npm:@nuxt/content-edge@latest",
"@nuxt/module-builder": "latest",
"@nuxt/test-utils": "latest",
"@nuxt/test-utils": "^3.0.0-rc.8",
"@nuxtjs/eslint-config-typescript": "latest",
"codecov": "latest",
"eslint": "latest",
"jsdom": "^20.0.0",
"nuxt": "npm:nuxt3@latest",
"@nuxt/content": "npm:@nuxt/content-edge@latest",
"standard-version": "latest"
"standard-version": "latest",
"vitest": "^0.22.1"
}
}
50 changes: 50 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useTestContext, $fetch } from '@nuxt/test-utils'
import { describe, test, expect, vi } from 'vitest'
import { mockedInfo } from 'consola'
import { r, setupNuxtTailwind } from './util'

describe('tailwindcss module', async () => {
vi.mock('consola', async () => {
const { default: mod } = (await vi.importActual<typeof import('consola')>('consola'))
mod.withScope = () => mod
mod.info = vi.fn()
return { default: mod, mockedInfo: mod.info }
})

await setupNuxtTailwind({
exposeConfig: true,
configPath: r('tailwind.config.js'),
cssPath: r('tailwind.css')
})

//
test('tailwind works', async () => {
const manifest = await $fetch('/_nuxt/manifest.json')
// @ts-expect-error untyped
const [, { css }] = Object.entries(manifest).find(([, v]) => v.isEntry)
const cssContents = await $fetch(`/_nuxt/${css[0]}`) as string

// test tailwind is the first entry
expect(cssContents.startsWith('/*! tailwindcss v')).toBeTruthy()
// test pages/index.vue is read
expect(cssContents).toContain('.max-w-7xl')
})

test('include custom tailwind.css file in project css', () => {
const nuxt = useTestContext().nuxt

expect(mockedInfo.mock.calls[0]).contains('Using Tailwind CSS from ~/tailwind.css')

expect(nuxt.options.css).toHaveLength(1)
expect(nuxt.options.css[0]).toEqual(nuxt.options.tailwindcss.cssPath)
})

// @todo re-implement
// test('custom paths', () => {
// const ctx = useTestContext()
//
// expect(logger.info).toHaveBeenNthCalledWith(1, `Using Tailwind CSS from ~/${relative(rootDir, nuxt.options.tailwindcss.cssPath)}`)
// expect(logger.info).toHaveBeenNthCalledWith(2, `Merging Tailwind config from ~/${relative(rootDir, nuxt.options.tailwindcss.configPath)}`)
// })
//
})
44 changes: 0 additions & 44 deletions test/config.test.ts

This file was deleted.

5 changes: 5 additions & 0 deletions test/fixture/basic/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
<NuxtPage />
</div>
</template>
5 changes: 5 additions & 0 deletions test/fixture/basic/malformed-tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
plugins: [
require('something-that-doesnt-exist')
]
}
11 changes: 11 additions & 0 deletions test/fixture/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { resolve } from 'path'
import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
alias: {
'@nuxtjs/tailwindcss': resolve(__dirname, '../../../src/module.ts')
},
modules: [
'@nuxtjs/tailwindcss'
]
})
18 changes: 18 additions & 0 deletions test/fixture/basic/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<template>
<!-- This example requires Tailwind CSS v2.0+ -->
<div class="bg-gray-50">
<div class="px-4 py-12 mx-auto max-w-7xl sm:px-6 lg:py-16 lg:px-8 lg:flex lg:items-center lg:justify-between">
<h2 class="text-3xl font-extrabold tracking-tight text-gray-900 sm:text-4xl">
<span class="block">Ready to dive in?</span>
<span class="block text-indigo-600">Start your free trial today.</span>
</h2>
<div class="flex mt-8 lg:mt-0 lg:shrink-0">
<div class="inline-flex rounded-md shadow">
<a href="/_tailwind" class="inline-flex items-center justify-center px-5 py-3 text-base font-medium text-white bg-indigo-600 border border-transparent rounded-md hover:bg-indigo-700">
Open Tailwind Config Viewer
</a>
</div>
</div>
</div>
</div>
</template>
4 changes: 4 additions & 0 deletions test/fixture/basic/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
// https://v3.nuxtjs.org/concepts/typescript
"extends": "./.nuxt/tsconfig.json"
}
15 changes: 15 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { fileURLToPath } from 'node:url'
import { setup } from '@nuxt/test-utils'

export const r = (s: string = '') => fileURLToPath(new URL('./fixture/basic/' + s, import.meta.url))

export const setupNuxtTailwind = (tailwindcss = {}) => {
return setup({
rootDir: r(),
server: true,
browser: false,
nuxtConfig: {
tailwindcss
}
})
}
23 changes: 23 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable spaced-comment */
/// <reference types="vitest" />
/// <reference types="vitest/globals" />

import { resolve } from 'path'
import { defineConfig } from 'vite'

export default defineConfig({
resolve: {
alias: {
'@nuxtjs/tailwindcss': resolve(__dirname, 'src/module.ts')
}
},
define: {
__VUE_OPTIONS_API__: 'true',
__VUE_PROD_DEVTOOLS__: 'false'
},
test: {
globals: true,
environment: 'jsdom',
reporters: 'dot'
}
})

0 comments on commit c6c103b

Please sign in to comment.