Test selection finds tests for NodeNext .js imports and Rails specs (#4679)

In TypeScript repos that import `./x.js` for `x.ts` (NodeNext) and in
Rails repos, touched source files now get their tests instead of none.

Why: `/verify` and `/ship` found no tests for these files even though
tests covered the files (#4398, #4211).

Nothing to do: a Rails file maps only when its spec exists at the
conventional path.

Co-authored-by: mikhailsal <14850941+mikhailsal@users.noreply.github.com>
Co-authored-by: Ikraam Ghoor <34813339+ikraamg@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1v/adversarial-native#1 reader=adversarial-native
Review-Finding: p1v/adversarial-native#2 reader=adversarial-native
Review-Finding: p1v/adversarial-native#3 reader=adversarial-native
Review-Finding: p1v/outside-voice#2 reader=outside-voice
Review-Finding: p1v/outside-voice#3 reader=outside-voice
This commit is contained in:
authored and GitHub committed 2026-11-22 12:32:40 +00:00
1 parent 7530280407
commit 4bd15f8a3d
5 files changed
+113 -4

No files matched your search

+28
View File
@@ -303,3 +303,31 @@ describe('import graph: alias precedence, extends and the no-lane explanation',
expect(text).toContain('declare the file under dependencies in .gstack/pregate.json, or report the resolver gap');
});
});
describe('import graph: NodeNext specifiers written with the emitted extension (#4398)', () => {
// Value: protects=NodeNext `./x.js` specifiers map to x.ts or x.tsx (and .jsx→.tsx, .mjs→.mts), directly and transitively, the
// import resolves to the .ts source over a .js twin as in tsc (the twin keeps its literal edge), and a filesystem literal naming
// a `.js` directory keeps that directory;
// fails_when=the .js→.ts rewrite is removed, runs after the as-written lookup, or applies to literals;
// why_new=no case covered emitted-extension imports; seam=none; no_claim=bundler-specific resolution beyond the TS rewrite
test('a NodeNext import written with the emitted .js extension maps to its .ts source', () => {
const repo = fixture();
commit(repo, {
'src/leaf.ts': 'export const leaf = 1;\n',
'src/mid.ts': "import { leaf } from './leaf.js';\nexport const mid = leaf;\n",
'src/view.tsx': 'export const View = () => null;\n',
'src/esm.mts': 'export const esm = 1;\n',
'src/both.js': 'export const both = 1;\n',
'src/both.ts': 'export const both = 2;\n',
'src/panel.tsx': 'export const Panel = () => null;\n',
'tests/fixtures/config.js/a.json': '{}\n',
'tests/fixtures/config.ts': 'export const config = 1;\n',
'tests/fixture.ts': "export { mid } from '../src/mid.js';\n",
'tests/app.test.ts': "import { readdirSync } from 'node:fs';\nimport { mid } from './fixture.js';\nimport { View } from '../src/view.jsx';\nimport { esm } from '../src/esm.mjs';\nimport { both } from '../src/both.js';\nimport { Panel } from '../src/panel.js';\nvoid [mid, View, esm, both, Panel, readdirSync('tests/fixtures/config.js')];\n",
});
const g = buildGraph(repo.work);
for (const f of ['tests/fixture.ts', 'src/mid.ts', 'src/leaf.ts', 'src/view.tsx', 'src/esm.mts', 'src/both.ts', 'src/both.js', 'src/panel.tsx', 'tests/fixtures/config.js/a.json']) expect(testsFor(g, f).tests).toEqual(['tests/app.test.ts']);
expect(testsFor(g, 'src/both.ts').provenance).toEqual(['tests/app.test.ts ← src/both.ts (import)']);
expect(testsFor(g, 'tests/fixtures/config.ts').tests).toEqual([]);
});
});
+60
View File
@@ -0,0 +1,60 @@
/**
* Rails autoloads constants, so a Ruby source names no require a spec could
* follow. The RSpec convention maps it instead: `app/<path>.rb` and
* `lib/<path>.rb` select `spec/<path>_spec.rb` and `spec/lib/<path>_spec.rb`
* when that spec exists, at the repo root or under an engine's own root.
*/
import { afterEach, describe, expect, test } from 'bun:test';
import { buildGraph, testsFor } from '../lib/pregate/import-graph';
import { cleanup, commit, makeFixtureRepo, type FixtureRepo } from './helpers/pregate-fixture';
const repos: FixtureRepo[] = [];
const fixture = () => { const r = makeFixtureRepo(); repos.push(r); return r; };
afterEach(() => { while (repos.length) cleanup(repos.pop()!); });
describe('import graph: RSpec specs mirror app/ and lib/', () => {
// Value: protects=a Rails source under app/ or lib/ selects the spec at its conventional path, though no require names it;
// fails_when=the RSpec convention edge is removed; why_new=Ruby sources had no test lane in a Rails repo (#4211);
// seam=none; no_claim=running RSpec (graph only)
test('app/, a class-named directory and lib/ files select the spec at the conventional path', () => {
const repo = fixture();
commit(repo, {
'app/services/plugins/weather.rb': 'class Plugins::Weather; end\n',
'app/services/plugins/weather/api.rb': 'class Plugins::Weather::Api; end\n',
'app/models/user.rb': 'class User; end\n',
'lib/parser.rb': 'class Parser; end\n',
'spec/rails_helper.rb': '# loads the app\n',
'spec/services/plugins/weather_spec.rb': "require 'rails_helper'\nRSpec.describe Plugins::Weather do; end\n",
'spec/services/plugins/weather/api_spec.rb': "require 'rails_helper'\nRSpec.describe Plugins::Weather::Api do; end\n",
'spec/models/user_spec.rb': "require 'rails_helper'\nRSpec.describe User do; end\n",
'spec/lib/parser_spec.rb': "require 'rails_helper'\nRSpec.describe Parser do; end\n",
});
const g = buildGraph(repo.work);
expect(testsFor(g, 'app/services/plugins/weather.rb').tests).toEqual(['spec/services/plugins/weather_spec.rb']);
expect(testsFor(g, 'app/services/plugins/weather/api.rb').tests).toEqual(['spec/services/plugins/weather/api_spec.rb']);
expect(testsFor(g, 'app/models/user.rb').tests).toEqual(['spec/models/user_spec.rb']);
expect(testsFor(g, 'lib/parser.rb').tests).toEqual(['spec/lib/parser_spec.rb']);
});
test('a spec under an engine or package root selects the source under that root', () => {
const repo = fixture();
commit(repo, {
'engines/billing/app/models/invoice.rb': 'class Invoice; end\n',
'engines/billing/lib/billing.rb': 'module Billing; end\n',
'engines/billing/spec/models/invoice_spec.rb': 'RSpec.describe Invoice do; end\n',
'engines/billing/spec/lib/billing_spec.rb': 'RSpec.describe Billing do; end\n',
});
const g = buildGraph(repo.work);
expect(testsFor(g, 'engines/billing/app/models/invoice.rb').tests).toEqual(['engines/billing/spec/models/invoice_spec.rb']);
expect(testsFor(g, 'engines/billing/lib/billing.rb').tests).toEqual(['engines/billing/spec/lib/billing_spec.rb']);
});
test('no edge when the conventional spec does not exist', () => {
const repo = fixture();
commit(repo, {
'app/models/order.rb': 'class Order; end\n',
'spec/models/user_spec.rb': 'RSpec.describe User do; end\n',
});
expect(testsFor(buildGraph(repo.work), 'app/models/order.rb').tests).toEqual([]);
});
});