@@ -7,7 +7,13 @@ import { Environment } from '../file-system.js'
77import { NodeFS } from '../node/file-system.js'
88import { Project } from '../project.js'
99
10- import { Accuracy , DetectedFramework , mergeDetections , sortFrameworksBasedOnAccuracy } from './framework.js'
10+ import {
11+ Accuracy ,
12+ type DetectedFramework ,
13+ VersionAccuracy ,
14+ mergeDetections ,
15+ sortFrameworksBasedOnAccuracy ,
16+ } from './framework.js'
1117import { Grunt } from './grunt.js'
1218import { Gulp } from './gulp.js'
1319import { Hexo } from './hexo.js'
@@ -180,6 +186,94 @@ describe('detect framework version', () => {
180186 } )
181187} )
182188
189+ describe ( 'detected framework version accuracy' , ( ) => {
190+ test ( 'should mark pinned version from package.json with medium accuracy' , async ( { fs } ) => {
191+ const cwd = mockFileSystem ( {
192+ 'package.json' : JSON . stringify ( { devDependencies : { '@11ty/eleventy' : '2.0.0' } } ) ,
193+ } )
194+ const project = new Project ( fs , cwd )
195+ const detection = await project . detectFrameworks ( )
196+ expect ( detection ) . toHaveLength ( 1 )
197+ expect ( detection ?. [ 0 ] . detected . package ?. versionAccuracy ) . toBe ( VersionAccuracy . PackageJSONPinned )
198+ expect ( detection ?. [ 0 ] . toJSON ( ) . package ) . toMatchObject ( {
199+ name : '@11ty/eleventy' ,
200+ version : '2.0.0' ,
201+ versionAccuracy : VersionAccuracy . PackageJSONPinned ,
202+ } )
203+ } )
204+
205+ test ( 'should mark range version from package.json with low accuracy' , async ( { fs } ) => {
206+ const cwd = mockFileSystem ( {
207+ 'package.json' : JSON . stringify ( { devDependencies : { '@11ty/eleventy' : '^2.0.0' } } ) ,
208+ } )
209+ const project = new Project ( fs , cwd )
210+ const detection = await project . detectFrameworks ( )
211+ expect ( detection ) . toHaveLength ( 1 )
212+ expect ( detection ?. [ 0 ] . detected . package ?. versionAccuracy ) . toBe ( VersionAccuracy . PackageJSON )
213+ expect ( detection ?. [ 0 ] . toJSON ( ) . package ) . toMatchObject ( {
214+ name : '@11ty/eleventy' ,
215+ version : '2.0.0' ,
216+ versionAccuracy : VersionAccuracy . PackageJSON ,
217+ } )
218+ } )
219+
220+ test ( 'should mark version from node_modules with high accuracy' , async ( { fs } ) => {
221+ const cwd = mockFileSystem ( {
222+ 'package.json' : JSON . stringify ( { devDependencies : { '@11ty/eleventy' : '^2.0.0' } } ) ,
223+ 'node_modules/@11ty/eleventy/package.json' : JSON . stringify ( { version : '2.0.1' } ) ,
224+ } )
225+ const project = new Project ( fs , cwd )
226+ const detection = await project . detectFrameworks ( )
227+ expect ( detection ) . toHaveLength ( 1 )
228+ expect ( detection ?. [ 0 ] . detected . package ?. versionAccuracy ) . toBe ( VersionAccuracy . NodeModules )
229+ expect ( detection ?. [ 0 ] . toJSON ( ) . package ) . toMatchObject ( {
230+ name : '@11ty/eleventy' ,
231+ version : '2.0.1' ,
232+ versionAccuracy : VersionAccuracy . NodeModules ,
233+ } )
234+ } )
235+
236+ test ( 'should not set version accuracy if no version is detected' , async ( { fs } ) => {
237+ const cwd = mockFileSystem ( {
238+ 'package.json' : JSON . stringify ( { devDependencies : { '@11ty/eleventy' : 'latest' } } ) ,
239+ } )
240+ const project = new Project ( fs , cwd )
241+ const detection = await project . detectFrameworks ( )
242+ expect ( detection ) . toHaveLength ( 1 )
243+ expect ( detection ?. [ 0 ] . detected . package ?. versionAccuracy ) . toBeUndefined ( )
244+ expect ( detection ?. [ 0 ] . toJSON ( ) . package . versionAccuracy ) . toBeUndefined ( )
245+ } )
246+
247+ test ( 'should not set version accuracy for non-node.js frameworks' , async ( { fs } ) => {
248+ const cwd = mockFileSystem ( {
249+ 'config.rb' : '' , // Middleman framework (no npm dependencies)
250+ } )
251+ const project = new Project ( fs , cwd )
252+ const detection = await project . detectFrameworks ( )
253+ expect ( detection ) . toHaveLength ( 1 )
254+ expect ( detection ?. [ 0 ] . detected . package ) . toBeUndefined ( )
255+ expect ( detection ?. [ 0 ] . toJSON ( ) . package ) . toMatchObject ( {
256+ version : 'unknown' ,
257+ versionAccuracy : undefined ,
258+ } )
259+ } )
260+
261+ test ( 'should fall back to package.json accuracy in browser environment' , async ( { fs } ) => {
262+ const cwd = mockFileSystem ( {
263+ 'package.json' : JSON . stringify ( { devDependencies : { '@11ty/eleventy' : '^2.0.0' } } ) ,
264+ } )
265+ vi . spyOn ( fs , 'getEnvironment' ) . mockImplementation ( ( ) => Environment . Browser )
266+ const project = new Project ( fs , cwd )
267+ const detection = await project . detectFrameworks ( )
268+ expect ( detection ) . toHaveLength ( 1 )
269+ expect ( detection ?. [ 0 ] . detected . package ?. versionAccuracy ) . toBe ( VersionAccuracy . PackageJSON )
270+ expect ( detection ?. [ 0 ] . toJSON ( ) . package ) . toMatchObject ( {
271+ version : '2.0.0' ,
272+ versionAccuracy : VersionAccuracy . PackageJSON ,
273+ } )
274+ } )
275+ } )
276+
183277describe ( 'detection merging' , ( ) => {
184278 test ( 'return undefined if no detection is provided' , ( ) => {
185279 expect ( mergeDetections ( [ undefined , undefined ] ) ) . toBeUndefined ( )
0 commit comments