Skip to content

Commit

Permalink
fix(docx-to-vfile): add vitest and make sure it works in vite
Browse files Browse the repository at this point in the history
  • Loading branch information
tefkah committed Mar 27, 2023
1 parent 86e32fe commit 39e53bb
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 41 deletions.
5 changes: 1 addition & 4 deletions libs/reoff/docx-to-vfile/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
}
},
"test": {
"executor": "@nrwl/jest:jest",
"executor": "@nrwl/vite:test",
"outputs": ["coverage/libs/reoff/docx-to-vfile"],
"options": {
"jestConfig": "libs/reoff/docx-to-vfile/jest.config.js",
"passWithNoTests": true
}
},

"build": {
"executor": "better-nx-tsc:tsc",
"outputs": ["{options.outputPath}"],
Expand Down Expand Up @@ -97,7 +95,6 @@
"access": "public"
}
},

"github-standalone": {
"executor": "@jscutlery/semver:github",
"options": {
Expand Down

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions libs/reoff/docx-to-vfile/src/lib/docx-to-vfile-unzipit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { docxToVFile } from './docx-to-vfile-unzipit.js'
import fs from 'fs'
import { describe, expect, it } from 'vitest'
import { fileURLToPath } from 'url'

describe('reoffDocxToVfile', () => {
const doc = fs.readFileSync(
new URL('../../../reoff-parse/src/test/word-citation.docx', import.meta.url),
)
const doc = //fs.readFileSync(
fileURLToPath(new URL('../../../reoff-parse/src/test/word-citation.docx', import.meta.url))
// )

const docimg = fs.readFileSync(new URL('../fixtures/images.docx', import.meta.url))
jest.setTimeout(10000)
it('should work', async () => {
const vfile = await docxToVFile(new Uint8Array(doc))
const vfile = await docxToVFile(doc)
const url = new URL('../fixtures/test.xml', import.meta.url)
fs.writeFileSync(url, String(vfile))
expect(vfile).toMatchSnapshot()
})
it('should contain vfile with relations data object', async () => {
const vfile = await docxToVFile(new Uint8Array(doc))
const vfile = await docxToVFile(doc)
const url = new URL('../fixtures/testrelations.xml', import.meta.url)
fs.writeFileSync(url, String(vfile))
expect(vfile.data.relations).toBeDefined()
})
it('should contain images', async () => {
const vfile = await docxToVFile(new Uint8Array(docimg))
const vfile = await docxToVFile(docimg)
const url = new URL('../fixtures/testimages.xml', import.meta.url)
fs.writeFileSync(url, String(vfile))
console.dir(vfile.data, { depth: null })
Expand Down
28 changes: 12 additions & 16 deletions libs/reoff/docx-to-vfile/src/lib/docx-to-vfile-unzipit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,19 @@ export interface DocxVFile extends VFile {
* @returns A VFile with the contents of the document.xml file as the root, and the contents of the other xml files as data.
*/
export async function docxToVFile(
file: ArrayBuffer | File | Blob | Buffer | ReadStream | string,
file: ArrayBuffer | File | Blob | Buffer | string,
userOptions?: Options,
): Promise<VFile> {
let input = file

// node code
if (typeof window === 'undefined') {
const { createReadStream, ReadStream } = await import('fs')
const { blob } = await import('stream/consumers')
const { Blob: NodeBlob } = await import('buffer')

const inp = typeof file === 'string' ? createReadStream(file) : file

input = (
inp instanceof ReadStream
? await blob(inp)
: inp instanceof ArrayBuffer
? new NodeBlob([Buffer.from(inp)])
: inp
) as Blob
if (process.env) {
const { readFile } = await import('fs/promises')
const { Buffer } = await import('buffer')

const inp = typeof file === 'string' ? await readFile(file) : file

input = Buffer.isBuffer(inp) ? new Blob([inp]) : file
}

const options: Options = {
Expand All @@ -124,7 +117,10 @@ export async function docxToVFile(
...userOptions,
}

const { entries } = await unzip(input as Blob)
console.log(input)
console.log(input instanceof Blob)

const { entries } = await unzip(input)
const rels = await entries['word/_rels/document.xml.rels'].text()
const relations = Object.fromEntries(
// eslint-disable-next-line regexp/no-super-linear-backtracking
Expand Down
5 changes: 4 additions & 1 deletion libs/reoff/docx-to-vfile/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
{
"path": "./tsconfig.spec.json"
}
]
],
"compilerOptions": {
"types": ["vitest"]
}
}
24 changes: 12 additions & 12 deletions libs/reoff/docx-to-vfile/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"types": ["jest", "node"]
"outDir": "../../dist/out-tsc",
"types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"]
},
"include": [
"**/*.test.ts",
"**/*.spec.ts",
"**/*.test.tsx",
"**/*.spec.tsx",
"**/*.test.js",
"**/*.spec.js",
"**/*.test.jsx",
"**/*.spec.jsx",
"**/*.d.ts",
"jest.config.ts"
"vite.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.test.tsx",
"src/**/*.spec.tsx",
"src/**/*.test.js",
"src/**/*.spec.js",
"src/**/*.test.jsx",
"src/**/*.spec.jsx",
"src/**/*.d.ts"
]
}
32 changes: 32 additions & 0 deletions libs/reoff/docx-to-vfile/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference types="vitest" />
import { defineConfig } from 'vite'

import viteTsConfigPaths from 'vite-tsconfig-paths'

export default defineConfig({
cacheDir: '../../../node_modules/.vite/docx-to-vfile',

plugins: [
viteTsConfigPaths({
root: '../../../',
}),
],

// Uncomment this if you are using workers.
// worker: {
// plugins: [
// viteTsConfigPaths({
// root: '../../../',
// }),
// ],
// },

test: {
globals: true,
cache: {
dir: '../../../node_modules/.vitest',
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
},
})

0 comments on commit 39e53bb

Please sign in to comment.